In this section, we will be discussing remote event. Remote event is an object that can be used to communicate through client to server and server to client. When you are scripting, some cases need communication between the client and server such as sending a game pass prompt when you touched a part.
There are some limits on the remote event. Please read after you finished the section by go .
Now, let's say we got 2 scripts. One is the server script located in ServerScriptService and one is the client script (LocalScript) located in StarterGui. We also insert one remote event in ReplicatedStorage and name it MyRemote.
The remote event should be placed where the server and client can access it such as ReplicatedStorage.
Placing the remote event in ServerStorage will take the ability of the client script (LocalScript) to access it and only the server can access it since the only script that can access through ServerStorage is a server script, not a client script (LocalScript).
SERVER USAGE
Client
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local myRemote = ReplicatedStorage:WaitForChild('MyRemote')
-- Communicate the server from the client by using :FireServer() and pass in data to send to the server from the client.
myRemote:FireServer('This is a client message')
Server
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local myRemote = ReplicatedStorage:WaitForChild('MyRemote')
myRemote.OnServerEvent:Connect(function(player, data)
-- Received a message from the client and triggered the block code below.
-- The first argument of OnServerEvent is always a local player.
print(data) -- This will print "This is a client message"
end)
When sending an instance from client to server make sure it exists on the server or else the instance you send will become nil on the server.
CLIENT USAGE
Server
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local myRemote = ReplicatedStorage:WaitForChild('MyRemote')
players.PlayerAdded:Connect(function(player)
-- Sending the message to the client when a player joins the game just for them.
MyRemote:FireClient(player, 'This data is from the server.')
end)
Client
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local myRemote = ReplicatedStorage:WaitForChild('MyRemote')
myRemote.OnClientEvent:Connect(function(serverData)
-- The client will be triggered code block here when :FireClient() or :FireAllClients() was called.
print(serverData) -- This will print "This data is from the server."
end)
ALL CLIENTS USAGE
Server
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local myRemote = ReplicatedStorage:WaitForChild('MyRemote')
players.PlayerAdded:Connect(function(player)
-- Sending the message to all players (clients) when a player joins the game.
MyRemote:FireAllClients('This data is from the server.')
end)
Client
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')
local myRemote = ReplicatedStorage:WaitForChild('MyRemote')
myRemote.OnClientEvent:Connect(function(serverData)
-- The client will be triggered code block in here when :FireClient() or :FireAllClients() was called.
print(serverData) -- This will print "This data is from the server."
end)