RemoteEvent

✏ Beginners Scripting Guide ✏

https://create.roblox.com/docs/reference/engine/classes/RemoteEvent 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.

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.

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)

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)

Last updated