RemoteEvent
RemoteEvent : Instance
Overview
A RemoteEvent is an Object used to handle events between the client and server, supporting asynchronous, one-way communication.
Since it needs to be accessible from both the server and the client, it should be placed in a shared location such as ReplicatedStorage or Workspace.
Properties
Methods
FireServer
This method is used when a client triggers the event connected to a server-side RemoteEvent.
When the event is triggered, the connected function receives the Player Object of the client that sent the request as its first argument.
Parameters
Tuple
Arguments
These are the arguments passed to the function connected to the OnServerEvent of a RemoteEvent.
You can pass basic Luau data types such as number, boolean, string, and table.
Objects like Instances can also be passed.
However, not all data types are fully supported, so it's recommended to test how they behave before using them.
Return
void
Code Samples
-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local C2S_EnterPlayer = ReplicatedStorage:WaitForChild("C2S_EnterPlayer")
local SomeNumber = 10
C2S_EnterPlayer:FireServer(SomeNumber) -- Passing arguments
-----------------------------------------------------------------------------------
-- Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local C2S_EnterPlayer = ReplicatedStorage:WaitForChild("C2S_EnterPlayer")
local function OnEnterPlayer(player, number)
print("[OnEnterPlayer]", player.Name, " / Parameter : ", number)
end
C2S_EnterPlayer.OnServerEvent:Connect(OnEnterPlayer)
FireClient
This method is used by the server to trigger the event connected to a specific client's RemoteEvent.
The Player argument must be provided to specify the target client.
If you want to trigger the event for all clients, it's more appropriate to use FireAllClients().
Parameters
Player
Player
The Player that will trigger the client's event.
Tuple
Arguments
These are the arguments passed to the function connected to the OnClientEvent of a RemoteEvent.
You can pass basic Luau data types such as number, boolean, string, and table.
Objects like Instances can also be passed.
However, not all data types are fully supported, so it's recommended to test how they behave before using them.
Return
void
Code Samples
-- Script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")
local SomeText = "FireClient"
local function EnterPlayer(player)
S2C_DeadEvent:FireClient(player, SomeText) -- Passing arguments
end
Players.PlayerAdded:Connect(EnterPlayer)
-----------------------------------------------------------------------------------
-- LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")
local function OnDeadEvent(text)
print("[OnDeadEvent]", "Parameter : ", text)
end
S2C_DeadEvent.OnClientEvent:Connect(OnDeadEvent)
FireAllClients
This method is used by the server to trigger the event connected to a RemoteEvent for all clients.
Since the event is sent to all clients, no Player argument is required.
Parameters
Tuple
Arguments
These are the arguments passed to the function connected to the OnClientEvent of a RemoteEvent.
You can pass basic Luau data types such as number, boolean, string, and table.
Objects like Instances can also be passed.
However, not all data types are fully supported, so it's recommended to test how they behave before using them.
Return
void
Code Samples
-- Script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")
local function EnterPlayer(player)
local SomeText = "FireAllClients"
S2C_DeadEvent:FireAllClients(SomeText) -- Passing arguments
end
Players.PlayerAdded:Connect(EnterPlayer)
-----------------------------------------------------------------------------------
-- LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")
local function OnDeadEvent(text)
print("[OnDeadEvent]", "Parameter : ", text)
end
S2C_DeadEvent.OnClientEvent:Connect(OnDeadEvent)
Events
OnClientEvent
When FireClient() or FireAllClients() is called on a RemoteEvent instance, all functions connected to its OnClientEvent are run, and the arguments provided during the call are passed along as-is.
Parameters
Code Samples
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")
local function OnDeadEvent(text)
print("[OnDeadEvent]", "Parameter : ", text)
end
S2C_DeadEvent.OnClientEvent:Connect(OnDeadEvent)
OnServerEvent
When FireServer() is called on a RemoteEvent instance, all functions connected to its OnServerEvent are run, and the arguments provided during the call are passed along as-is.
Parameters
Code Samples
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local C2S_EnterPlayer = ReplicatedStorage:WaitForChild("C2S_EnterPlayer")
local function OnEnterPlayer(player, number)
print("[OnEnterPlayer]", player.Name, " / Parameter : ", number)
end
C2S_EnterPlayer.OnServerEvent:Connect(OnEnterPlayer)
See also
Server-Client CommunicationLast updated