I don't fully understand remote events

I don’t fully understand what remote events are and before you tell me, if I have checked other posts and videos, I have.

I watched Alvinblox’s video on remote events and kinda understood them.

I think I understand that like that because of FE that you can’t communication from the client to the server and you have to use remote events because of that. Remote events like help you talk between the client and the server ( I don’t really understand what that means, if someone can clarify, would be much appreciated).

I also used a remote event in like an example, I thought wouldn’t work but somehow worked.

Localscript inside tool:

local PS = game:GetService("Players")
local player = PS.LocalPlayer
local tool = script.Parent	

tool.Activated:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)

ServerScript :

local players = game:GetService("Players")


players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local strength = Instance.new("IntValue")
	strength.Parent = leaderstats
	strength.Name = "Strength"
	
	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
		strength.Value = strength.Value + 25
		wait(0.5)
	end)
	
end)

I don’t really understand the purpose of a remote event and what people mean by like communication between a client and server. If someone can explain to me and help clarify, would be much appreciated. Thank you! :slightly_smiling_face:

8 Likes

In this case, based on your code provided, whenever you activate your tool, you are firing that event, which will be connected to any server script based on “OnServerEvent” this will then run your strength addition (btw you can do strength.Value += 25) this will run every time, you can pass values through these events

The main purpose of using remote functions and/or events is to “tell” the server or “tell” the client whatever kind of data you want, an example is using a event to tell a client what to load for an inventory that was made on the serverside’s datastores

-- CLIENT
local Strength = 25
game.ReplicatedStorage.RemoteEvent:FireServer(Strength)
-- SERVER
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, 
Strength)
 Strength += 25
 print(player, Strength) -- will print the updated strength, you can then pass this to update some data you are using to keep track of player's strength
-- player will always be the first variable passed
-- you can change variables names across events, you can do (player, whateverHere)
end)

You can also use the code below to fire to a specific client or all clients

-- SERVER
game.ReplicatedStorage.RemoteEvent:FireClient(player, data) -- must specify the player followed by whatever data to send
-- CLIENT
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(player, recievedData)
 print(player, recievedData)
end)

In the case you want to fire to ALL clients, you can use RemoteEvent:FireAllClients()

2 Likes

So the way most games work, you have lots of clients which have a copy of the game (each player’s computer is a client), and a server which has a copy of the game (Roblox’s computer that runs the whole thing). These are all different computers with their own copies of the game.

As people play the game, they change things on their own client. Say I move forward a stud. My client tells the server the changes, which it uses to update its own copy of the game, then tells everyone else’s computers what to update too. That way everyone knows that I’ve moved forward a stud, and everyone has the same copy of the game. Roblox handles all that in the background, and Filtering makes sure that the server, the main copy of the game that everyone else copies from, doesnt copy everything from every client. Just the things your computer is actually allowed to change, like where your character is.

Remote events let you send your own messages and updates between everyone’s computers. If I fire (send) a remote event from my client (the computer I’m playing on), to the server (Roblox’s computer), I’m sending a message between the computers with any information it might need. The server can then fire another event to individual player’s computers, or to all player’s computers. That’s all remotes are, sending information between computers.

So with your code when you use your tool, your computer uses the remote to tell the server “Hey! I just used my tool!”. The server then changes the leaderstats, and Roblox tells everyone else’s computers the new leaderstats. Perfect! A great way to use remote events.

So servers and clients are just ways of saying Roblox’s computer and players’ computers, and remote events let you talk between the computers. Make sense?

6 Likes

So from what I understand from your reply:

  • Every player’s computer or whatever is a client.
  • There is a computer on Roblox’s side which is a server that has a copy of the game.
  • Some things are allowed to be sent to the server such as where your character is but not everything.
  • Remote events lets you do some of those things that you aren’t allowed to do.
  • A remote events sends a message or request from the client like in my case “hey, I used my tool, update my leaderstats”, to the server and the server can fire that event on all the clients.
  • So you can’t change leaderstats from the client so you have to ask the server to do it and you basically send the info of that you used the tool and then the server updates your leaderstats for everyone’s clients.

Also, I know this is not related to the topic but how can I like access the leaderstats variable like

local PS = game:GetService("Players")

PS.PlayerAdded:Connect(function(plr)
    local leaderstats = instance.new("Folder")
    leaderstats.parents = plr
    leaderstats.Name = "leaderstats"
end)

How can I access that variable from outside the event, if it is possible.

2 Likes

That’s exactly right! And you can use that basic idea of sending messages between computers to make more complicated things.

A gun system is just your client telling the server the things you’re doing, the server making sure you’re allowed to do those changes like shooting a gun, and then making changes and telling everyone else’s clients about them. It’s all just computers talking between each other.

Once you understand what remotes do and how to use them, a good idea would be to learn how to secure a remote. Securing remotes means to add checks on the server to make sure that your client is actually telling the truth. But that’s a topic for another time, make sure you get comfortable using remotes and understanding how to use them before making your remotes even more complicated.

2 Likes

As for your question, every time you receive an event on the server it tells you what player it’s from. Because you’ve made a leaderstats folder in every player, you just need to figure out what player you’re getting a message from and change their specific folder.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)) will tell you exactly which player sent the message.

From there use the player variable to find the stats folder - local stats = player:FindFirstChild(“leaderstats”)

And then the value - local Strength = stats:FindFirstChild(“Strength”)

Then just change the value - Strength.Value += 25

That leaves you with the function

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
local stats = player:FindFirstChild(“leaderstats”)
local Strength = stats:FindFirstChild(“Strength”)
Strength.Value += 25
end)

That’s a really nice way of doing things, because you only have one function connected to the event for every player, instead of a function for every player that runs everytime anyone fires the event.

1 Like

So the first parameter of the is always the player because I watched a yt tutorial on it and one of the vids said that the first parameter is always the player no matter what you call it.

Also, I was going to ask, what are some ways I can practice using remote events like what can I create that is simple but uses remote events and doesn’t use remote events in a complex way? I can’t really think of anything.

Maybes an announcement system could help? A player types a message into a GUI, tells the server the message, and then use the server to tell everyone else the message and display that on their GUI. That way you can practice sending mesages from Client to Server, but also Server to Client too?

1 Like

Yeah, I’ll try create an announcement system, like you said where a player types a message into a GUI and the server puts that message on everyone’s GUI. Might be a bit hard for me but I will try.

To be fair, you don’t even need a GUI. Just make your client send a message like “[username] knows how to use remotes!”, print the message on the server, and then send and print it on all the other clients without worrying about GUIs. Same remotes, just keeps things simple.

1 Like

What do you mean by making my client send a message? You mean like a print a message?

Your client send a message to the server, for example; you want to show that a player has finished the obby; so you have a RemoteEvent which the client will send a message to the server and the server will retrieve the message and inform other players to show the finished gui; you’d need to do sanity checks to avoid exploiters from well… exploiting your remotes

hol’ up I’ll go to paint to make a visual

4 Likes

Yeah, but like the problem is like I don’t know where I am meant to send the fireserver part?

Like I tried right now with my current knowledge, it didn’t work.

Local script :

local part = game.Workspace.Part
local textlabel = script.Parent.TextLabel

part.Touched:Connect(function(hit)
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)

Server Script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	textlabel.Text = "Player finished the obby"
end)

Comes with this:

ServerScriptService.Script:2: attempt to index nil with ‘Text’

I think I am meant to add a parameter in the fireserver part but like not sure, I tried with a parameter. Didn’t actually know what that would do, didn’t work.

thats, because theres no variable textlabel defined, if you want to code the illustration:

-- server
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	-- ... do some checks

    game.ReplicatedStorage.RemoteEvent:FireAllClients(player.Name)
end)
-- client
local part = game.Workspace.Part
local textlabel = script.Parent.TextLabel

part.Touched:Connect(function(hit)
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(playerName)
   textlabel.Text = `{playerName} finished the obby` -- assuming textlabel is defined
end
1 Like

i don’t understand what is happening there everything after the fireserver() part and in the server part, how do you use :FireAllClients?

:FireAllClients() does not necessarily require any arguments, but you can pass in as many arguments as roblox can allow; its basically :FireClient() but without the player argument

1 Like

It’s just listening for a “show the gui” request

1 Like

I still want to create something that only uses Fireserver() and OnServerEvent. What do you think is something I can create that only uses both of those things.

Maybe a gun shooting mechanic? where the client sends the position of the mouse in 3d space and the server spawns a bullet facing the target (im really bad at coming up with projects)

or you can make a team changer where the client sends a message to the server to switch to a team with the provided team name/color, and the server validates the message and switches the player team

1 Like

another example is that you can imagine this as ordering a nachos in a restaurant; first you go up to the cashier and request the nachos (which is equivalent to sending a message to the server); and then you wait until your nachos is done; in which the cashier will send a message back by calling your name to pick up your order

1 Like