Leaderstats obby saving value but not spawning player at spawn location after leaving

Hiya guys! Welcome to another episode of “struggling with simple concepts”!!!

Jokes aside, I have a obby that uses leader stats to save. Now everything works great, when a player dies they respawn at their checkpoint and stuff, and the game saves their value. Now your thinking, well, it all seems right, but no, it’s not!

When the player leaves it saves their value, but when they join it doesn’t spawn the player in their value’s spawn point. For example, if I leave with my leaderstat value equalling 2, it will still spawn me at respawn pad 1, but the value will still have saved.

Character loading script, this is what makes the player spawn at pads related to leaderstats:

local checkpoints = workspace:WaitForChild("checkpoints")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats

	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)

		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)
	end)
end)

This is what saves the value:

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("Stage")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local success, errorMessage = pcall(function()
		data= DataStore:GetAsync(player.UserId)
	end)
	
	if success then
		player.leaderstats.Stage.Value = data
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data
	local success, errorMessage = pcall(function()
		local data=DataStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
	end)
	
	if success then 
		print("Data saved")
	else 
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, v in pairs(game.Players:GetPlayers()) do
		v:Kick()
		print("Server shut down")
	end
	wait(2)
end)

No matter what the player’s value is, it only spawns at the first pad.

image

2 Likes

Are you sure the value is saving when the player leaves?

Mhm, it saves perfectly. If the value is 2, then it will save 2!

You got 2 scripts with 2 different PlayerAdded events? If so, can you combine the 2 scripts into 1?

Well yes, but it’s much easier not to as they are designed for 2 completely different things.

Well, the stage value might not have completely loaded when you try to get it with the other script though.

Ok, I see now. a few people have said that the character is probably loading before the stage leaderstats is. How would I move the PlayerAdded function to the other playeradded function though?

Edit: Just added one script to the other a boom, it works perfectly!