Remote Events

Tutorial on Remote Events and how to use them

by kitrank

Author Avatar

What's a Remote Event

Remote Events are objects that allow client to server and vice verse communication. You fire them from a local script which then runs a function in a server script when they are fired, or vice versa. They are very practical nowadays, ever since Filtering Enabled has been enforced on developers.

How to use Remote Events (Client to Server)

To demonstrate, we will instance a new Part on the Server activated from a local script. Firstly, insert a Remote Event into ReplicatedStorage. ReplicatedStorage is storage accessible by both the Client and Server which is how they work. They would not work in ServerStorage due to it being accessible by the Server only.

You also need to insert a LOCAL SCRIPT and SERVER SCRIPT to interact with each

other. LOCAL SCRIPT Step 1: Define Replicated Storage. This is also done in the server script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

LOCAL SCRIPT Step 2: FireServer function

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.RemoteEvent:FireServer()

This is the function that will fire the Event in the SERVER SCRIPT. Additionally, you can include ARGUEMENTS in the function that will be sent over to the server. The player who fired the Remote Event is automaticlly sent over, so no need to send that, but we will get to that soon. Say you wanted to send a number value over, you could do that like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.RemoteEvent:FireServer(1000000)

-- You can just add the value, AND/OR send a variable already declared:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Num = 1000000

ReplicatedStorage.RemoteEvent:FireServer(Num)

SERVER SCRIPT Step 1: Defining ReplicatedStorage (Again)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

SERVER SCRIPT Step 2: OnServerEvent Event

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.OnServerEvent:Connect(function(Player, Num)
print(Player)
-- Output is the player who fired the Remote Event.

print(Num)
-- Output: 1000000
-- Has nothing to do with creating a Part on the Server, just an example of sending
-- variables/arguments to the server. Now for instancing a Part, incase you didn't
-- know how to yet:
local Part = Instance.new("Part")
Part.Parent = workspace
print(Player .. " successfully created a part from their client!")
end)

You do not need to include arguments in the OnServerEvent function, however if you do, the Player always comes first, then the arguments you sent over in the FireServer function in their order. They also do not need to be named identically in either of the scripts, however it's less confusing if you name them the same thing. For example, you could name the Player as "Plr" and you could name the Num argument in just the SERVER SCRIPT as "something" but they would still be the same.

Server to Client(s)

Server to Client is a bit different. You can either fire to a specific client, or all of them at once. Going to assume you did Step 1 already and just get on to Step 2, SERVER SCRIPT though:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage:FireClient(Player)
-- Must include the Player when firing a single client.

-- Or, just fire all clients:
ReplicatedStorage:FireAllClients()

LOCAL SCRIPT Step 3: OnClientEvent Event

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
end)

Conclusion

Hopefully this tutorial was helpful in both explaining and showing you how to use Remote Events. If so, please leave a rating, otherwise leave a comment if something wasn't explained properly. Thanks for reading.

View in-game to comment, award, and more!