I’m a really new developer and I need help with server side and client sides. One thing that I’m not clear about is if I need to fire the remoteEvent from a local player Do I need to add the remoteEvent in the localScript?
You can fire a remote event from either the server or the client,
Client is a local script
Server is a script
If you fire a remote event from the client, the server has to be the receiving end.
--local script
RemoteEvent:FireServer() -- Sends
-- script
RemoteEvent.OnServerEvent:Connect(function(Player) -- Recieves
-- Do what you need
end)
If you fire a remote event from the server, the client has to be the receiving end.
-- script(Server)
RemoteEvent:FireClient(Player --[[The player you're firing too]]) -- Sends
-- local script (client)
RemoteEvent.OnClientEvent:Connect(function() -- Recieves
-- Do what you need
end)
The remoteevent must be in a place where the server and the client can both access it. Remoteevents are typically stored in replicatedstorage
Hey!
Remote Events are pretty easy to understand, so let me try explain.
Remote Events can make comunicate between Scripts and Local Script or Local Scripts and Scripts.
An example can be:
if you press a button in a part in workspace (script) you make a gui show to all players, and to do that you can easily do:
Script.Parent.MouseClick:Connect(function() -- I used a ClickDetector
-- you can even send informations, such variables
game.ReplicatedStorage.RemoteEvent:FireAllClients("hello")
end)
-- SCRIPT in workspace.
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(information)
script.Parent.Visible = true
script.Parent.Name = information -- hello
end)
so when you click the button in workspace, it will send a message, that activate all local script connected to the same remote event, and make an action you want.
And if you want to communicate between local, script, local you use a Remote Function and for communicate between script and script a Bindable Event.
And if you want to communicate between a local script and a script you use the Remote Events, like before.
Lastly, if you need Player Information (name or ecc) you can use a local script to send to a script.
Client-Server architecture is simple, Clients and Server both are software Clients are your actual game on your computer that is connecting to the server, Server, as name suggests, serves their clients, the game, The actual game, is present on server, all the client sees what server tells them the environment is, suppose you want your character to go forward, Server cant detect clients input as they both are different machines, you give input to the game (Client). Client calculates where your character has moved, then it informs the server. Simple example!
Remote Functions - Scripting Tutorial
This stuff is actually really easy … don’t let it overwhelm you.
Welcome to the powerful, beautiful, and painful world of programming!
To answer your question in simple terms, Yes, you can add a RemoteEvent in a LocalScript but it’s not recommended in the slightest as it’s very difficult and problematic to locate.
A reasonable and recommended place where you should place a RemoteEvent is inside of ReplicatedStorage! ReplicatedStorage is a ServiceProvider that stores items such as Scripts, Objects, and more. It replicates its contents to the server and client, and it’s accessible from the client and server in scripts.
Going back to your question, if you’d like to send information to the server from the client, you would have to fire the RemoteEvent from a LocalScript.
-- Refer to the ReplicatedStorage Service in order to grab the RemoteEvent to use in the script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.RemoteEvent
-- Once you've referenced the RemoteEvent inside of ReplicatedStorage, You'd want to use the :FireServer() event, and pass any information you'd want the server to retrieve
remoteEvent:FireServer(informationToServer) -- Replace 'informationToServer' with what you'd like to send
Example of firing the RemoteEvent from a LocalScript
Now that we’ve sent the information to the server, let’s get that information and use it from a Server Script!
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.RemoteEvent
-- To retrieve the information sent by the client, You'd have to use the .OnServerEvent event
remoteEvent.OnServerEvent:Connect(function(player, informationYouSent) -- The first argument will always be the player (LocalPlayer) who sent the information to the server
-- Do something with the information on the server
end)
Example of the Server receiving the information from the client in a Script
I hope this helps you in understanding the Client-Server communication a little bit more and how to use RemoteEvents! I’ll provide you some resources to understand the topic more and enhance your skills!
Resources
Remote Events & Callbacks
Client-Server Runtime
ReplicatedStorage
RemoteEvent