I want it to save whatever team the player is on for when they rejoin. When I print what team they are on, it says “None”, even though I was assigned to the “Lobby” team in my game.
Here is my code:
game.Players.PlayerRemoving:Connect(function(plr)
-- Store player's current team in a variable before the player is removed
local playerTeam = plr.Team and plr.Team.Name or "None"
-- Save player's team
print("Player's current team (stored): " .. playerTeam)
if playerTeam ~= "None" then
local teamSucc, teamErr = pcall(function()
teamData:SetAsync(plr.UserId .. "-team", playerTeam)
end)
if teamSucc then
print("Team successfully saved for player " .. plr.Name)
else
warn("Error with saving team: " .. teamErr)
end
else
print("Player " .. plr.Name .. " has no team to save.")
end
end)
That’s because when a player is leaving they get removed from whatever team they’re in so it’s always nil. What I would do is set an attribute whenever the player changes teams because attributes don’t get deleted until the parent instance is fully deleted
You’re correct. I would use something like this: Player:GetPropertyChangedSignal("Team"):Connect(function(player) local playerTeam = player.Team end)
Make sure you’re able to define player beforehand. Also, if you’re doing it on the server, maybe use a table and store players, or modify how the variable is saved.