Every time I’m coding something in this function, it feels like there’s only such limited things I can do. And if I want to do something that involves the Client, I can’t because it always seems that Client is already called nil before the function fires.
I’m not trying to do anything, I’ve just worked on scripts in the past that involved the PlayerRemoving function when I had to get a bit technical, but the output most of the time listed the existing player as nil, therefore breaking the function.
I know this function is very useful when storing a Client’s data, if they left the game or crashed, but most of the time it feels like the player is called nil before the function even passes.
Is this function preferred to be in the beginning of the script? Because I can understand any wait delay can cause this sort of problem. I’m just interested in if this function has any limitations, or if not, how it can be done right.
There was a similar issue with PlayerAdded not firing properly in Studio when a lot of variables were defined before it, try using this sort of connection
local function OnRemoving(player)
-- define variables required in this scope
end
Players.PlayerRemoving:Connect(OnRemoving)
-- as opposed to an anonymous function and defining all variables before the connection
Players.PlayerRemoving:Connect(function(player) end)
Also in cases where you need to save data for a player, immediately get the key - for instance the UserId, as the event fires so you won’t have issues with the player being called nil after you handle a lot of unnecessary functions before you can attempt to save.