Hey, as the title states, I do not understand how Remote Events work. I've tried the Roblox wiki and other sites. I really hate having to come to ask a question about them, but alas I have to. I've tried watching tutorials and other things. I really need help, can someone explain it to me, please?
Remote events (and functions, but you only asked for events) allow for client <-> server communication. And never on the same machine. That is, no server to server or client to client. A client can get something to happen on the server side, and the server can get something to happen on the client side.
From the server side you'd listen for the OnServerEvent
event.
local remoteEvent = Instance.new("RemoteEvent") remoteEvent.Parent = game:GetService("ReplicatedStorage") remoteEvent.OnServerEvent:Connect(function(client, ...) print(client, "sent the following:", ...) end)
What is this client
parameter? The first parameter is always the client who fired the remote event
. Here it's just printing any arguments received to the listener.
From the client side you would call the FireServer
function of remote event:
local remoteEvent = ... -- you know what to do remoteEvent:FireServer("Hi", "Hello", "Awesome!")
It is a common mistake to pass the player as first argument but this is not needed.
Similar to OnServerEvent
but you use OnClientEvent
. Note that you don't need to worry about any default parameters because the server fires this event and not a client
.
remoteEvent.OnClientEvent:Connect(function(...) print("the server sent the following:", ...) end)
From the server side, you'd call FireClient
local Players = game:GetService("Players") local remoteEvent = ... -- you know what to do Players.PlayerAdded:Connect(function(client) remoteEvent:FireClient(client, "Hi!", "Cool", "Amazing") end)
It is important that the first argument is the client for the server to fire to
! Or else Roblox won't know who to work with. In addition, if you want to fire to all clients use the FireAllClients
function
remoteEvent:FireAllClients(...)
You don't need to pass any player since it's literally just firing to everyone.
Client-Server model
When players join a game, a server is started on a computer in Roblox headquarters. This computer stores stuff like maps that are in workspace and other scripts in ServerStorage and ServerScriptService.
On the player's computer, a client is started and this client is what you play on and it has stuff like GUIs and localscripts.
we call Roblox's version the server, and the players version the client.
multiple clients connect to a server so that you can play multiplayer, and the server sends a copy of the map to each client so that you can see the game.
your client can send stuff to the server and vice-versa so that players can interact. but this comes with a problem...
Exploiters
Since the client connects to the server, an exploiter could send stuff that the game developer didn't want to send to the server. This can allow exploiter to destroy games and get free items and in-game currency etc.
in order to prevent this Roblox does not allow the client and server to communicate unless the developer intends it to by using RemoteEvents
How they work
RemoteEvents can be fired from server to client or client to server. when a remote event is fired from client to server the client sends a signal to the server telling it that an event has been fired, but it does not affect it.
when the event is fired, a script on the server might check it and do something important for the game to function.
Code
this example prints the player's name on the server so all players can see it.
Client
local event = game.ReplicatedStorage.RemoteEvent --assigns the remote event to a variable event:FireServer() -- this fires the event/sends a signal to the server
Server
local event = game.ReplicatedStorage.RemoteEvent --assigns the remote event to a variable event.OnServerEvent:Connect(function(player) -- when the event is fired, run this function print(player) end)
you can add more parameters and incapaz's answer explains how to use it better
Locked by User#24403
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?