If the wiki page didn't help you, then here is a "quick" explanation of how RemoteEvents and Functions work.
#RemoteEvents and functions
So basically, with FE, a LocalScript isn't allowed to change any instances that the server replicates, like things in the workspace. If you try, then other players won't see that change, but you will, which will cause confusion. [However, that can be put to good use, for example, if you want only one player to see something, or be able to go through a door, as seen in Egg Hunt 2018, but that's another story.]
Also, a regular [server] script can't change stuff in a PlayerGui with FE. If you try that, it will work in studio, but not in game
So how would you allow a LocalScript to change the baseplate color from grey to red? Well, you can't do it directly from a LocalScript, otherwise, only the player will see it, and nobody else will. The answer to that is a remote event,
Without Remote Events:
- LocalScript changes baseplate to red
- Baseplate color changes for the player
- Signal sent to server, which tells the server to have all the other players see a red baseplate.
- Server rejects it because it was sent by the player, and FE is on.
- Player sees a red baseplate, but everyone else sees a grey one.
- People are confused.
But using Remote Events/functions
- LocalScript fires Remote Event
- Remote event sends signal to script
- Script signal sent to server, which tells the server to have all the other players see a red baseplate.
- Server accepts request, because it is by a server script, ("Script").
- Server replicates (copies) the signal to all players, telling their computer to turn the baseplate red
- All players see a red baseplate
- Everyone is happy.
As for one of your questions, it is recommended to put server scripts in the ServerScriptService, unless they are meant to be parented to a brick or something. Server Scripts only run in the workspace or ServerScriptService.
And typically, put RemoteEvents and functions in ReplicatedStorage.
Here is an example.
#Client to Server communication
#RemoteEvents
Make a RemoteEvent in the ReplicatedStorage named BaseplateChanger
LocalScript
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote:FireServer()
wait(5)
print("Scripting is fun")
Script in ServerScriptService
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote.OnServerEvent:Connect(function(player)
workspace.Baseplate.BrickColor = BrickColor.new("Bright red")
wait(3)
end)
#RemoteFunctions
Make a RemoteFunction in the ReplicatedStorage named BaseplateChanger
LocalScript
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote:FireServer()
wait(5)
print("Scripting is fun")
Script in ServerScriptService
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote.OnServerInvoke = function(player)
workspace.Baseplate.BrickColor = BrickColor.new("Bright red")
wait(3)
end
OnServerInvoke is a callback, so you write the function differently, and InvokeServer is a YieldFunction; it waits for the function to finish, so it yields (waits). Also, only one function can be connected to that callback.
That was a simple use of remote events and functions. Obviously, you're not gonna use them to make the baseplate red, but let's talk about that "player" parameter you see on line 4 of the script. The first parameter is the player who fired that event or invoked the function. All other arguments come next. remember that, or you might get a function that talks about a "userdata value" and be very confused.
That was server to client communication. What about the other way around?
#Server to Client communication
Like I said before, the server can't access the playergui, so the server needs to send a signal to the client
#RemoteEvent
LocalScript (PlayerGui (StarterGui) > ScreenGui > TextLabel > LocalScript)
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote.OnClientEvent:Connect(function()
script.Parent.Text = "I like to script"
wait(3)
end)
Script in ServerScriptService
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote:FireClient(player)
remote:FireAllClients()
wait(5)
print("done")
#RemoteFunction
LocalScript
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote.OnClientInvoke = function()
script.Parent.Text = "I like to script"
wait(3)
end)
Script in ServerScriptService
local remote = game:GetService("ReplicatedStorage"):WaitForChild("BaseplateChanger")
remote:InvokeClient(player)
wait(5)
print("done")
1 comment
Thank you very much!