The script i have below is a script where after the leader of a colony in my game leaves, it waits 10 seconds (because of grouped colony teleporting) before selecting a new leader. The thing is, on the 2nd line of the script I cant get anything inside the Player, if I attempt to it will show an error.
game.Players.PlayerRemoving:Connect(function(LeavingPlayer)
local colonyleadervalue = LeavingPlayer.PlayerGui.ColonyGui.YourColonyFrame:FindFirstChild("ColonyLeaderValue")
task.wait(10)
while task.wait(0.1) do
print("check")
for i, player in pairs(game.Players:GetPlayers()) do
if colonyleadervalue then
if player.Name ~= colonyleadervalue.Value and LeavingPlayer.Character.Colony.Value == player.Character.Colony.Value then
local rng = math.random(1, 50)
if rng == 50 then
colonyleadervalue.Value = player.Name
return
end
end
end
end
end
end)
You can’t. If you need some information about a player after they’ve left (PlayerRemoving fires after the player has actually disconnected), you need to have stored it somewhere that’s not a child of their Player instance. Normally, I would use just a Lua table in a server script that is a dictionary keyed by Player.UserId. You will always be able to get the UserId of a player on PlayerRemoving, and it will remain valid to use as a dictionary key even after their Player object is gone.
The only thing you have to be careful about is handling the case where the same user rejoins a server they were on previously; you have to make sure that you’re not holding onto data from their previous session that is not valid for their current session.