Hello! I have some questions regarding remote events’ behaviour when sending signals.
Here are the components at play:
Client Sided Script inside Starter Gui.
Server Sided Script inside ServerScriptService.
RemoteEvent named “RemoteEvent” inside ReplicatedStorage.
On the Server Sided Script, I am running a function whenever a player joins the game, which prints “Player joined!” and then fires the remote event to that player.
On the Client Sided Script, I am waiting for 5 seconds using the task library, printing “Client script ran!”, then connecting the remote event to a function which prints “Connection ran!”.
Server Sided Script:
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
print("Player connected!")
remoteEvent:FireClient(Player)
end)
(Assume the 5 second yield operation starts immediately, meaning there is no significant delay before the client loads the client script.)
Intuitively, what I believe should happen is the server PlayerJoined connection function should print “Player connected!”, then the client sided script yields for 5 seconds and prints “Client script ran!”.
I exclude the possibility of the client connection function ever running because the Remote Event fires before the connection registers on the client.
The Remote Event connection function runs and I am remain confused.
Please let me know why this happens, thank you.
You can wait for the player’s character to load and only then trigger the event. And in LocalScript, remove the delay.
Here is the code
local remoteEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“RemoteEvent”)
local Players = game:GetService(“Players”)
Players.PlayerAdded:Connect(function(Player)
print(“Player connected!”)
local character = Player.Character or Player.CharacterAdded:wait()
remoteEvent:FireClient(Player)
end)
Perhaps I didn’t explain it all well, I am more interested in why the remote event connection function runs, even though the event was fired before the connection was registered on the client.
I was a bit confused at first, but I get what you mean now. Basically, considering the remote is fired before the OnClientEvent is connected, it shouldn’t be picked up. I did some poking around and found out why.
To put it simply, Roblox is quite diligent on babysitting remotes. (that’s why they created a whole different variant called Unreliable Remote Events, but that’s another tangent).
This is because Roblox wants to ensure your remote event gets heard on both sides, and it uses methods like these to ensure its delivery. In your case, once the server fires to the client, the Event signal is queued until the client script can catch up. I don’t know if that’s because the signal explicitly waits for each script, or it knows it’s yielding for a connection. Regardless, Roblox queued it so it could get the signal.
I didn’t know myself, so this was an interesting question. Thank you.
Post side note: I’m pretty sure this is done considering sometimes setting up client-listeners can be slow, and Roblox doesn’t want the setup for clients to miss out on something important.