Save teams doesn't work

Hi, I have a script where I want it to save the player’s team whenever they leave.

For example if they were on green and left, when they rejoined they would still be on green.

However, when I leave and rejoin, it still keeps teleporting me to lobby team, and I have no idea why.

Could anyone please help?

local dss = game:GetService("DataStoreService")
local deathData = dss:GetDataStore("deathData")
local teamData = dss:GetDataStore("teamData")

game.Players.PlayerAdded:Connect(function(plr)

	local lb = Instance.new("Folder", plr)
	lb.Name = "leaderstats"


	local deaths = Instance.new("IntValue", lb)
	deaths.Name = "Deaths"


	local death
	local succ, err = pcall(function()
		death = deathData:GetAsync(plr.UserId.."-deaths")
	end)

	if succ and death ~= nil then
		deaths.Value = death
	else
		deaths.Value = 0 
		warn("Error with getting deaths: " .. err)
	end

	local savedTeamName
	local teamSuccess, teamErr = pcall(function()
		savedTeamName = teamData:GetAsync(plr.UserId.."-team")
	end)

	if teamSuccess and savedTeamName then
		local savedTeam = game.Teams:FindFirstChild(savedTeamName)
		if savedTeam then
			plr.Team = savedTeam
			print("Restored team for player " .. plr.Name .. ": " .. savedTeamName)
		else
			plr.Team = game.Teams.Lobby 
			print("Saved team not found, assigning player to Lobby.")
		end
	else
		plr.Team = game.Teams.Lobby
		print("No saved team found, assigning player to Lobby.")
	end

	plr.CharacterAdded:Connect(function(char)
		local teamName = plr.Team and plr.Team.Name
		if teamName then
			for _, checkpoint in pairs(workspace:FindFirstChild("Checkpoints"):GetChildren()) do
				if checkpoint.Name == teamName then
					local hrp = char:FindFirstChild("HumanoidRootPart")
					if hrp then
						hrp.CFrame = checkpoint.CFrame + Vector3.new(0, 2, 0)
						print("Teleported player to checkpoint: " .. checkpoint.Name)
					end
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	-- Save deaths
	local succ, err = pcall(function()
		deathData:SetAsync(plr.UserId.."-deaths", plr.leaderstats.Deaths.Value)
	end)

	if succ then
		print("Deaths successfully saved for player " .. plr.Name)
	else
		warn("Error with saving deaths: " .. err)
	end

	-- Save player's team
	if plr.Team then
		local teamSucc, teamErr = pcall(function()
			teamData:SetAsync(plr.UserId.."-team", plr.Team.Name)
		end)

		if teamSucc then
			print("Team successfully saved for player " .. plr.Name)
		else
			warn("Error with saving team: " .. teamErr)
		end
	end
end)
2 Likes

Try using game:BindToClose to save all the players data right before the server closes, assuming you tested it in studio, which is only 1 player and therefore the local server would shut down right after you leave

2 Likes

The server disappears after at least running all functions that operate when the player leaves.
Elseif there is no wait() things.
aaand elseif server crashed

Is there any errors on Output?
Does it not work well even in the studio?

These informations will make it easier for us to find the problem and answer.

Could you elaborate what are you trying to point out? I’m pretty sure PlayerRemoved doesn’t realiably fire upon server closing (correct me if im wrong), it’s not about functions, it’s about events

I think theres a problem with this line, try printing plr.Team, since Team could be nil

1 Like

In fact, servers don’t disappear immediately when there are 0 players. This is a fact you can check.
As I said, the PlayerRemoving function works without a problem unless there is a wait(+a) in the function or the server crashes.

Even more, game.OnClose() function is deprecated.

2 Likes

Yeah, I think the first SetAsync (deathData:SetAsync(plr.UserId.."-deaths", plr.leaderstats.Deaths.Value)) is causing it to yield longer than it needs to be. Which I assume causees the Team property to be nil

One way to solve this is to store the team before the first SetAsync. Another way is to just use one table for each player

not talking about that, i was talking about game:BindToClose

oh I see. I didn’t know they added another one

anyway, I tested his script and found the problem.

it is nil. i mean, plr.Team is nil as @Artzified said.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.