Does a player object always exist when sending through remotes

When I send a player object through a remote, is it possible that in the time it takes for the data to be sent from the server to the client, the player has left the game and the player object no longer exists? I’m wondering if I should send the UserId instead of the player object and check if the player is still in the game…

If an instance is parented to a place that is not replicated to the other party (in this case, nil), then nil will be sent. Why is the client sending a player to the server in the first place?

Oh wait, it’s the other way around. The server is sending the player to the clients

The rule of “Instances parented in a place not replicated to the other party are sent as nil” is still true.

While this is true, they are referring to explicitly sending an additional player as an argument.

2 Likes

Note that replication is ordered, so in most cases you can assume an instance sent over remotes from the server to the client is never nil.

If a player leaves right after the server fires an event with a reference to that player’s instance as an additional parameter, the event will also be processed on the client before the client removes the player locally.

Things like this will thus always work:

-- server:
local somePlayer = game.Players:GetPlayers()[1]
event:FireAllClients(somePlayer)

-- client:
event.OnClientEvent:Connect(function(thatPlayer)
    -- thatPlayer is never nil (unless the client removed it from the datamodel itself...)
end)

Be careful when sending a reference to instances that may be removed from the datamodel when you send the reference to the player to clients, though:

-- server:
local somePlayer = game.Players:GetPlayers()[1]
wait() -- somePlayer might leave the game during this yield, i.e., be removed from the datamodel
print('can we still safely transfer somePlayer?', game:IsAncestorOf(somePlayer))
event:FireAllClients(somePlayer) --

-- client:
event.OnClientEvent:Connect(function(thatPlayer)
    -- thatPlayer might be nil
end)

(When sending Player instances from the client to the server you’ll never know for sure if the server will receive a valid reference, though.)

This holds for any instance, by the way (just make sure the server actually fully controls the existence of instances you send over the network; be for example careful with any parts simulated by physics as they can be controlled by clients).

7 Likes