OnClientEvent Fires Instantly After Connecting

Hello,

I noticed that if RemoteEvent:FireClient() is ran before the player connects using OnClientEvent, then the next time the player connects using OnClientEvent it will be run immediately. This caused me some issues when trying to disconnect the connection.

Here is an example:

--In server script
local Remote = Instance.new("RemoteEvent")
Remote.Name = "MyRemote"
Remote.Parent = game.ReplicatedStorage
Remote.OnServerEvent:Connect(function(Player)
	Remote:FireClient(Player)
end)

--In Client Script
local Remote = game.ReplicatedStorage:WaitForChild("MyRemote")
Remote:FireServer()
wait(1)
local Connection 
Connection = Remote.OnClientEvent:Connect(function()
	Connection:Disconnect()	-- This will error because Connection is nil
end)

This only happens if Workspace.SignalBehavior is set to Default

1 Like
Remote.OnClientEvent:Once(function()

end)

:Once() method exists

1 Like

I am unable to find documentation on this, but it is known and often desirable. If you are having trouble with it, you may be able to timestamp events and discard ones that are too early for your purpose.

This is just an acknowledgment announcement!

We’ve filed a ticket to our internal database, and we’ll follow up when we have an update!

Thanks for the report!

1 Like