How Come My Save Script for checkpoints Isnt working?

I am trying to make a program where the player’s checkpoints are saved. When the player rejoins, they get teleported back to the last stage they were on.

Sometimes the chekpoints do work

And sometimes it teleports you to stage 0

I have no idea why this is happening and I need help

Scripts for saves:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("StagesForPlayer")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local success, errorMessage = pcall(function()
		data = myDataStore: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()
		data = myDataStore: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:GetChildren()) do
		v:Kick("Server Closed")
	end

	wait(2)
end)

Scripts for checkpoints:

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 = 0
	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)

Anything is greatly appreciated!

1 Like

You misspelled local at local checkpoints = workspace:WaitForChild("Checkpoints").

Oops thats a copying error (Its spelled normally in game)

Do you move to the checkpoint after resetting the character?

Yes I move back when I die. It only happens when you join the game

I had the same problem with my obby game too, try adding wait(3) instead of wait()

Combine both your scripts together so that its synced.

Try this: (delete both scripts and make a new one)

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local myDataStore = DataStoreService:GetDataStore("StagesForPlayer")
local checkpoints = workspace:WaitForChild("Checkpoints")

--//Functions
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 = 0
	stage.Parent = leaderstats
	
	local data = nil
	
	local success, errorMessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success then
		stage.Value = data or 0
	else
		warn(errorMessage)
	end
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		
		task.wait()
		char:MoveTo(checkpoints[stage.Value].Position)

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

Players.PlayerRemoving:Connect(function(player)	
	local success, errorMessage = pcall(function()
		myDataStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
	end)

	if success then
		print("Data Saved")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, player in ipairs(Players:GetPlayers()) do
		player:Kick("Server Closed")
	end
end)
1 Like

Thanks dude it actually fixed it. I changed wait(3) to wait(0.5) so it is more seamless.

Alright, good to hear that you fixed it. I hope your obby become popular :grin:.