RemoteEvent
A RemoteEvent is designed to provide a one-way message between the server and clients, allowing Scripts to call code in LocalScripts and vice-versa. This message can be directed from one client to the server, from the server to a particular client, or from the server to all clients.
In order for both the server and clients to utilize a remote event, the RemoteEvent object itself must be in a place where both sides can see it. As such, we recommend that you store the RemoteEvent inside of ReplicatedStorage, although in some cases it's appropriate to store it in the workspace or inside a Tool.
If you need the result of the call, you should use a RemoteFunction instead. Otherwise a remote event is recommended since it will minimize network traffic/latency and won't yield the script to wait for a response. For more information, see Remote Events and Functions.
Code Samples
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent")
createPartEvent.Name = "CreatePartEvent"
createPartEvent.Parent = ReplicatedStorage
local function onCreatePartFired(player, color, position)
print(player.Name, "wants to create a part")
local newPart = Instance.new("Part")
newPart.BrickColor = color
newPart.Position = position
newPart.Parent = workspace
end
createPartEvent.OnServerEvent:Connect(onCreatePartFired)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = ReplicatedStorage:WaitForChild("CreatePartEvent")
createPartEvent:FireServer(BrickColor.Green(), Vector3.new(10, 20, 0))
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local welcomePlayerEvent = Instance.new("RemoteEvent")
welcomePlayerEvent.Parent = ReplicatedStorage
welcomePlayerEvent.Name = "WelcomePlayerEvent"
local function onPlayerAdded(player)
welcomePlayerEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local welcomeMessage = Instance.new("TextLabel")
welcomeMessage.Size = UDim2.new(0, 200, 0, 50)
welcomeMessage.Parent = welcomeScreen
welcomeMessage.Visible = false
welcomeMessage.Text = "Welcome to the game!"
local function onWelcomePlayerFired()
welcomeMessage.Visible = true
task.wait(3)
welcomeMessage.Visible = false
end
welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)
local Players = game:GetService("Players")
local newPlayerEvent = Instance.new("RemoteEvent")
newPlayerEvent.Parent = game.ReplicatedStorage
newPlayerEvent.Name = "NewPlayer"
local function onPlayerAdded(_player)
newPlayerEvent:FireAllClients()
end
Players.PlayerAdded:Connect(onPlayerAdded)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local newPlayerEvent = ReplicatedStorage:WaitForChild("NewPlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local newPlayerMessage = Instance.new("TextLabel")
newPlayerMessage.Size = UDim2.new(0, 200, 0, 50)
newPlayerMessage.Parent = welcomeScreen
newPlayerMessage.Visible = false
newPlayerMessage.Text = "A new player has joined the game!"
local function onNewPlayerFired()
newPlayerMessage.Visible = true
task.wait(3)
newPlayerMessage.Visible = false
end
newPlayerEvent.OnClientEvent:Connect(onNewPlayerFired)
Summary
Properties
Methods
Fires RemoteEvent.OnClientEvent for the specified player.
Fires the RemoteEvent.OnServerEvent event on the server using the arguments specified with an additional player argument at the beginning.
Events
Fires listening functions in LocalScript when either RemoteEvent:FireClient() or RemoteEvent:FireAllClients() is called from a Script.
Fires listening functions in Script when RemoteEvent:FireServer() is called from a LocalScript.
Properties
Methods
FireAllClients
The FireAllClients function fires the RemoteEvent.OnClientEvent event for each client.
Unlike RemoteEvent:FireClient(), this event does not take a target player as an argument. Instead it will fire to all clients who have the same remote event connected to an OnClientEvent event.
Since this function is used to communicate from the server to the client, it will only work when used in a Script.
The behavior of this function, as well as other RemoteEvent and RemoteFunction events and functions, is well documented in this article.
There are limitations on the kinds of data that can be passed between the client and server. For more information, see Argument Limitations.
- Data can be passed from server to client through remote events in the same way data is passed from client to server. Any extra information can be passed in as arguments to the RemoteEvent:FireClient() and FireAllClients functions. Note that the FireClient function still needs to pass the player to send the message to as the first argument.
Returns
Code Samples
local Players = game:GetService("Players")
local newPlayerEvent = Instance.new("RemoteEvent")
newPlayerEvent.Parent = game.ReplicatedStorage
newPlayerEvent.Name = "NewPlayer"
local function onPlayerAdded(_player)
newPlayerEvent:FireAllClients()
end
Players.PlayerAdded:Connect(onPlayerAdded)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local newPlayerEvent = ReplicatedStorage:WaitForChild("NewPlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local newPlayerMessage = Instance.new("TextLabel")
newPlayerMessage.Size = UDim2.new(0, 200, 0, 50)
newPlayerMessage.Parent = welcomeScreen
newPlayerMessage.Visible = false
newPlayerMessage.Text = "A new player has joined the game!"
local function onNewPlayerFired()
newPlayerMessage.Visible = true
task.wait(3)
newPlayerMessage.Visible = false
end
newPlayerEvent.OnClientEvent:Connect(onNewPlayerFired)
FireClient
FireClient causes OnClientEvent to be fired in LocalScripts running for the given Player. Additional data passed to this function is then provided to OnClientEvent; beware of limitations on this data.
Since this function is used for communication from server to client, so it will only work when used by a server-side Script. For client-to-server communication (the other direction), use FireServer. Direct client-to-client communication not possible on Roblox; however, it can be simulated using a Script that relays information received through some other means, such as FireServer.
There are limitations on the kinds of data that can be passed between the client and server. For more information, see Argument Limitations
See also:
- FireAllClients, which works similarly but for all Player
- Remote Functions and Events, which describes related classes, functions and events and also important limitations on the data that can be sent
- Sometimes a game will need to send information from one client to another. Roblox does not support direct client to client contact, so any communication must first go through the server. This is typically done using remote events (although functions could be used if desired). First, the sending client would call FireServer. On the server, the function connected to OnServerEvent would hear this firing, and itself would then call FireClient.
Returns
Code Samples
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local welcomePlayerEvent = Instance.new("RemoteEvent")
welcomePlayerEvent.Parent = ReplicatedStorage
welcomePlayerEvent.Name = "WelcomePlayerEvent"
local function onPlayerAdded(player)
welcomePlayerEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local welcomeMessage = Instance.new("TextLabel")
welcomeMessage.Size = UDim2.new(0, 200, 0, 50)
welcomeMessage.Parent = welcomeScreen
welcomeMessage.Visible = false
welcomeMessage.Text = "Welcome to the game!"
local function onWelcomePlayerFired()
welcomeMessage.Visible = true
task.wait(3)
welcomeMessage.Visible = false
end
welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)
FireServer
The FireServer event fires the RemoteEvent.OnServerEvent event on the server using the arguments specified with an additional player argument at the beginning.
Since this function is used to communicate from the client to the server, it will only work when used in a LocalScript.
When firing from the client note that nothing has to be passed in by default (unlike firing to the client from the server - where the player is passed in).
The functionality of this function, as well as other RemoteEvent and RemoteFunction events and functions, is well documented in this article.
There are limitations on the kinds of data that can be passed between the client and server. For more information, see Argument Limitations
Returns
Code Samples
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent")
createPartEvent.Name = "CreatePartEvent"
createPartEvent.Parent = ReplicatedStorage
local function onCreatePartFired(player, color, position)
print(player.Name, "wants to create a part")
local newPart = Instance.new("Part")
newPart.BrickColor = color
newPart.Position = position
newPart.Parent = workspace
end
createPartEvent.OnServerEvent:Connect(onCreatePartFired)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = ReplicatedStorage:WaitForChild("CreatePartEvent")
createPartEvent:FireServer(BrickColor.Green(), Vector3.new(10, 20, 0))
Events
OnClientEvent
The OnClientEvent event fires listening functions in LocalScript when either RemoteEvent:FireClient() or RemoteEvent:FireAllClients() is fired by the server from a Script.
This is used to retrieve remote events fired by the server and intended for the client. This event is in place to provide a method for communicating between the server and client, which is well documented in this article. This event retrieves remote events fired by the server to the client.
To fire from the client to the server, you should use RemoteEvent:FireServer() and RemoteEvent.OnServerEvent.
Code Samples
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local welcomePlayerEvent = Instance.new("RemoteEvent")
welcomePlayerEvent.Parent = ReplicatedStorage
welcomePlayerEvent.Name = "WelcomePlayerEvent"
local function onPlayerAdded(player)
welcomePlayerEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("WelcomePlayerEvent")
local playerGui = player:WaitForChild("PlayerGui")
local welcomeScreen = Instance.new("ScreenGui")
welcomeScreen.Parent = playerGui
local welcomeMessage = Instance.new("TextLabel")
welcomeMessage.Size = UDim2.new(0, 200, 0, 50)
welcomeMessage.Parent = welcomeScreen
welcomeMessage.Visible = false
welcomeMessage.Text = "Welcome to the game!"
local function onWelcomePlayerFired()
welcomeMessage.Visible = true
task.wait(3)
welcomeMessage.Visible = false
end
welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)
OnServerEvent
Fires listening functions in Script when RemoteEvent:FireServer() is called from a LocalScript.
This is used to retrieve remote events fired by the client and intended for the server. This event is in place to provide a method for communicating between the client and server, which is well documented in this article. This event retrieves remote events fired by the client to the server.
To fire from the server to the client, you should use RemoteEvent:FireClient() and RemoteEvent.OnClientEvent.
Code Samples
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent")
createPartEvent.Name = "CreatePartEvent"
createPartEvent.Parent = ReplicatedStorage
local function onCreatePartFired(player, color, position)
print(player.Name, "wants to create a part")
local newPart = Instance.new("Part")
newPart.BrickColor = color
newPart.Position = position
newPart.Parent = workspace
end
createPartEvent.OnServerEvent:Connect(onCreatePartFired)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = ReplicatedStorage:WaitForChild("CreatePartEvent")
createPartEvent:FireServer(BrickColor.Green(), Vector3.new(10, 20, 0))