Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

I don't understand how Remote Events work, I've tried the wiki and other places, help me please? [closed]

Asked by 5 years ago

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?

1
Will explain right now. Give me some time (people ninja me cus i write long as hell answers to these stuff generally) User#24403 69 — 5y
0
RemoteEvents is a way for client to connect to the server and the other way around. If you don't use RemoteEvents in the studio, it looks fine, but in the server if you see the other player doing it, it wouldn't do anything except the player body movement. MArzalAlBuchariZ 33 — 5y

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?

2 answers

Log in to vote
6
Answered by 5 years ago
Edited 5 years ago

RemoteEvents

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.

Server 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.

Firing to the server

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.

Client side

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)

Firing to the client

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.

2
Best Answer 2k19 AswormeDorijan111 531 — 5y
2
It’s always these you answer;) and always these upvoted, it’s as if you had the time to pre-write this:) Not bad Ziffixture 6913 — 5y
Ad
Log in to vote
1
Answered by
farizarps 132
5 years ago
Edited 5 years ago

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

0
uh i think you got something wrong with the exploiter section User#23365 30 — 5y
0
big time DeceptiveCaster 3761 — 5y
0
yeah lol also he didn't ask for a lecture on the client to server model just remote event... User#24403 69 — 5y