Remote Events In A Nutshell

Remote events are very important for game development, and although I am sure most of you are well versed in them, I decided to make a quick guide for those that aren’t!

First, what do remote events do? They allow you to transfer data through signals between the client and server end of your game. In other words, remotes allow client scripts to fire server functions, and server scripts to fire client functions.

To get started, simply make a client script anywhere in your game, a server script anywhere in your game, and a remote event in replicated storage. The remote does not HAVE to be there, but it is generally the best place to put it.

Client example:

Let’s say you want to make a button in a screen gui fire the server, with an amount of money you want the button to reward (like in a clicker simulator, but slightly less secure). To do so, simply create a local script with code along these lines:

Side note because someone was complaining, firing data from the client to the server in this manner, specifically sending the amount to reward from the client instead of already having the amount to reward in the server is a major vulnerability that could lead to hackers/exploiters easily giving themselves a lot of that reward, this is merely an example of how you can fire data, not the recommended usage of it.

local Button = script.Parent
local remote = game:GetService("ReplicatedStorage").RemoteEvent

local amountToReward = 1

Button.MouseButton1Up:Connect(function()
   remote:FireServer(amountToReward)
end)

A corresponding server script would look something like this:

local remote = game:GetService("ReplicatedStorage").RemoteEvent

remote.OnServerEvent:Connect(function(player, amountToReward)
   player.leaderstats.Money.Value = player.leaderstats.Money.Value + amountToReward
end)

In this example, the server received a message from the client. When firing the server, you only have to include extra variables, but on the server portion, the first variable in the function will be the player no matter what. The same is sort of true in an opposite way for if you fire the client from the server.

Server Example:

Let’s say a round in your game just ended, and you want a ui to appear on the screen of every player describing their stats for that round (win or lose, their rewards, kills, time survived, or anything else you want to display). This is pretty simple to accomplish, following the same concept as before, but this time starting from the server end.

Start by making a script that has some kind of function running when the match ends, doing this:

local remote = game:GetService("ReplicatedStorage").RemoteEvent

function matchEnded()
   local teamThatWon = nil
   -- put something here to determine which team won
   remote:FireAllClients(teamThatWon) -- fires to all players
   --OR
   remote:FireClient(player, teamThatWon) -- only fires the player you give it
end)

As you could see here, unless you fire all clients, you must include the player again, but this time, it doesn’t get the player FROM the function, but gives the player TO the function, so that it knows which specific client to fire.

On the client end of this example, you could create a script inside a screen gui that looks something like this:

local player = game:GetService("Players").LocalPlayer
local remote = game:GetService("ReplicatedStorage").RemoteEvent
local ui = script.Parent -- put the screen gui here

remote.OnClientEvent:Connect(function(teamThatWon)
   ui.Enabled = true
   if player.Team = game.Teams:FindFirstChild(teamThatWon.Name) then
      --put what happens when the player wins here
   else
      --put what happens when the player loses here
   end
end)

These code examples contain extra scripting for specific scenarios, but the basic idea remains the same, which is that remote events are very useful and important. Knowing how to use them is essential for game development.

TLDR: Use These

Client Code Samples:

local remote = game:GetService("ReplicatedStorage").RemoteEvent

remote:FireServer(variable)

remote.OnClientEvent:Connect(function(variable2)

end)

Server Code Samples:

local remote = game:GetService("ReplicatedStorage").RemoteEvent

remote:FireClient(player, variable2)

remote:FireAllClients(variable2)

remote.OnServerEvent:Connect(function(player, variable)

end)

More information about remote events can be found here.

Im sorry… what?
This is a vulnerability. An attacker could just run remote:FireServer(1234567890) and get as much award as they want. This is bad practice.

Never trust the client (to a degree ofcourse)

1 Like

I did mention that, it was merely an example of how the client can fire data to the server. I’ll make it a tad more clear that it is a vulnerability though.

Well, mention how bad of an idea it is. Dont promote doing it

2 Likes