How fixing saving checkpoints in obby

Hey guys, i don’t know how fix this problem.

when I join the game I appear on the spawn, but it shows me that I am at stage 45, but I want me to appear at the last stage where I reached

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData")


local function SavePlayerData(player)
	
	local success,errormsg = pcall(function()
	
		local SaveData = {}
		
		for i,stats in pairs(player.leaderstats:GetChildren()) do
			
			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId,SaveData)
	end)
	
	if not success then 
		return errormsg
	end			
end	


Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player
	
	local Stage = Instance.new("StringValue")
	Stage.Name = "Stage"
	Stage.Parent = Stats
	Stage.Value = 1
	
	local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
			
		print(Data.Stage)
		
		for i,stats in pairs(Stats:GetChildren()) do
			
			stats.Value = Data[stats.Name]		
		end			
	else		
		print(player.Name .. " has no data.")			
	end
	
	
	player.CharacterAdded:Connect(function(character)
		
		local Humanoid = character:WaitForChild("Humanoid")
		local Torso = character:WaitForChild("HumanoidRootPart")
		
		wait()
		
		if Torso and Humanoid then
			if Stage.Value ~= 0 then
				
				local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
				Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)					
			end	
		end	
	end)		
end)


Players.PlayerRemoving:Connect(function(player)
	
	local errormsg = SavePlayerData(player)
	
	if errormsg then	
		warn(errormsg)		
	end	
end)

game:BindToClose(function()
	for i,player in pairs(Players:GetPlayers()) do	
		
		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)```

This is because the object stages dosent exsists in workspace ill send the fix code in a while (pardon me for bad grammer and spelling)

1 Like
if Torso and Humanoid then
			if Stage.Value ~= 0 then
				
				local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
				Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)					
			end	
		end	

Error is in the variable called “StagePart”

Fixed Code:

if Torso and Humanoid then
			if Stage.Value ~= 0 then
				
				local StagePart = workspace:WaitForChild("Stages"):FindFirstChild(Stage.Value) -- If it still dosent work let me know
				Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)					
			end	
		end	

Just add WaitForChild()

This is because if a datastore script gives a error it outputs the last save it did

Weird, i put this, but i still show up at the spawn

aha thats cause you typed Stage.Value

if Torso and Humanoid then
			if Stage.Value ~= 0 then
				
				local StagePart = workspace:WaitForChild("Stages"):FindFirstChild(Stage) -- If it still dosent work let me know
				Torso.CFrame = StagePart.Value.CFrame + Vector3.new(0,1,0)					
			end	
		end	

a fix of a fix also u can do humanoidrootpart but its the same

I sent you a DM, can you please respond :slight_smile: