Help making obby checkpoints

Me and one of my friends are currently making an obby game. If you want to test it, the link to the game is here: Obby levels 1-10 TEST - Roblox. Anyway, I spent 3 days trying to figure out checkpoints and watching youtube videos of how to code it, and this was the one that worked: https://www.youtube.com/watch?v=c9lfIy5EyL8.

I was wondering how to make it save. Like, how do I make it so that if you leave the game, it keeps your checkpoint.

Thanks,
voidcrusher385

The code for the checkpoints is here:

local checkpointsFolder = game.Workspace.CheckpointsFolder

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(character)
		local humanoid = character:WaitForChild("Humanoid")
		wait()
		character:MoveTo(checkpointsFolder[stage.Value].Position)
		
		humanoid.Touched:Connect(function(hit)
			if hit.Parent == checkpointsFolder then
				if tonumber(hit.Name) == stage.Value + 1 then 
					stage.Value += 1
				end
			end
		end)
	end)
end)```
5 Likes

Hey there! To make the player’s checkpoints save, you should use the DatastoreService. This code should do just that! Ensure you have access to API services enabled (go to game settings, security). Let me know whether this works. If it throws any error let me know!

local DDS = game:GetService("DataStoreService")
local players_checkpoints = DDS:GetDataStore("Checkpoints")
local checkpointsFolder = game.Workspace.CheckpointsFolder

game.Players.PlayerAdded:Connect(function(player)
	local data = players_checkpoints:GetAsync(player.UserId) or {1}

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = data[1]
	stage.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		wait()
		character:MoveTo(checkpointsFolder[stage.Value].Position)
		
		humanoid.Touched:Connect(function(hit)
			if hit.Parent == checkpointsFolder then
				if tonumber(hit.Name) == stage.Value + 1 then 
					stage.Value += 1
				end
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data = {player.leaderstats.Stage.Value}
	players_checkpoints:SetAsync(player.UserId, data)
end)

game:BindToClose:Connect(function()
	for i, player in pairs(game.Players:GetChildren()) do
		local data = {player.leaderstats.Stage.Value}
		players_checkpoints:SetAsync(player.UserId, data)
	end
end)
4 Likes

Thank you so much! Just checking, I should put this in serverscriptservice, right? And I can name the script whatever? Sorry if this is a dumb question, I am new to lua

Yeah, that’s how leaderstats work!

1 Like

just use the DataStoreService to save and load the player checkpoint data

Okay, it’s not working for me. Does it only work when the game is public?

1 Like

go to the game settings - security and enable studio access to data store api

1 Like

Yeah uhh… you missed something

EDIT: Also, did you check the output for any errors?

1 Like

I did turn on access to API services, but I will check for output errors.

2 Likes

Can you show the line it happened in?

1 Like

Yes. Thank you so much for helping!

Instead of game:BindToClose:Connect, do game:BindToClose

That fixed the error, but it still doesn’t save when I quit the game.

Could you show me the stage’s value before you rejoined and after you rejoined?

Before i rejoined:


After:

Like, it just restarts me at the beginning like nothing happened

Also, all I did was put this script into serverscriptservice (I titled the script “Save”) and change the thing in settings. Was there something else I was supposed to do?

What script are you changing the stage’s value in?

EDIT: Oh yeah sorry, it’s in the data saving script

Another script in serverscriptservice. It is just called “Script”

That might actually be the issue