Help with a checkpoint system

So I’m trying to make a checkpoint system, sort of like that of an obby game, but it doesn’t seem to be working. I read tutorials and read the roblox documentation on checkpoints, yet I still can’t figure it out. This is a crucial part of my game I’ve been working on, so I appreciate any help.

By the way, my checkpoint value does update after each checkpoint I touch and it saves. The issue is: when I rejoin the game I don’t actually spawn at a checkpoint. I read this could have something to do with having a default roblox spawn point? I did try briefly removing the spawn point, still no luck though

Here’s my checkpoint handler script:

local checkpoints = game.Workspace.Checkpoints:GetChildren()

function gotoCheckpoint(character, checkpoint)
	local rootPart = character:WaitForChild("HumanoidRootPart")
	repeat wait(0.00) until rootPart
	
	for i, checkpoint in pairs(checkpoints) do
		print("Checkpoint".. tostring(checkpoint))
		if ("Checkpoint" ..tostring(checkpoint)) == checkpoint.Name then
			print("Check- poiiiiiiints fouwwwwwwwwnd")
			rootPart.CFrame  = checkpoint.CFrame * CFrame.new(0,1,0)
			
			break
		end
	end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	local CheckPoint = player:WaitForChild("Checkpoint")
	player.CharacterAdded:Connect(function(character)
		wait(0.01)
		gotoCheckpoint(character, CheckPoint.Value)
	end)
end)

for i, checkpoint in pairs(checkpoints) do
	checkpoint.Touched:Connect(function(touch)
		local hum = touch.Parent:FindFirstChild("Humanoid")
		
		if hum and hum.Health > 0 then
			local player = game:GetService("Players"):GetPlayerFromCharacter(touch.Parent)
			local CheckPoint = player.Checkpoint
			
			if player then
				if (tonumber(checkpoint.Name:match("%d+"))-CheckPoint.Value) == 1 then
					CheckPoint.Value += 1
				end
			end
		end
	end)
end

My datastore script is a different script, also worth knowing that i have a checkpoints folder in the workspace with 2 checkpoints “2” and “3” because the checkpoints value is 1 by default

3 Likes

Are there any logs in the ‘Output’, such as errors being unable to fetch players being added and moving their character to a specified location? If so please provide a screenshot.

2 Likes

No, there aren’t any errors which is why i’m really stumped

Thanks for replying btw

2 Likes

Are your debugging steps such as

print("Check- poiiiiiiints fouwwwwwwwwnd")

being logged?

If so, please could you alternatively provide your Datastore Manager script and the full script that is managing the checkpoints. I don’t seem to see how your datastore manager is fetching user data, such as their current checkpoint.

2 Likes

Well, i have the datastore in another script. As for the print functions, no. Some of them do not print

2 Likes

Seems like it’s an issue with fetching player data, if you could provide your datastore script that manages fetching player data I could better understand the issue.

2 Likes

This is what saves the data:

local DSS = game:GetService("DataStoreService")
local MyStore = DSS:GetDataStore("GOAWv1")

game.Players.PlayerAdded:Connect(function(player)
	wait()
	
	local playerKey = "id_"..player.UserId
	
	local saveValue1 = player.Coins
	local saveValue2 = player.Checkpoint
	
	local GetSaved = MyStore:GetAsync(playerKey)
	if GetSaved then
		saveValue1.Value = GetSaved[1]
		saveValue2.Value = GetSaved[2]
	else
		local valuestosave = {saveValue1.Value, saveValue2.Value}
		MyStore:GetAsync(playerKey, valuestosave)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local saveItems = {player.Coins.Value, player.Checkpoint.Value}
	MyStore:SetAsync("id_"..player.UserId, saveItems)
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetChildren()) do
		plr:Kick("All data has been saved! Yippe!")
	end
end)

leaderstats is also its own script, all values are saved in the player, not leaderstats though

2 Likes

i will try to explain it simply

  • give the player a stat when they join

  • make a folder for every checkpoint and name every checkpint 1,2,3,4,5 etc

  • when the player touchs a checkpoint check if the ( checkpoint name == (player.level + 1) ) if so then make the player level = the checkpoint name / player.level = toNumber(checkpoint.Name)

  • check for Player.Character.CharacterAdded:Connect() and teleport the player to the checkpoint smt like this

local Checkpoints = workspace.CheckPointsFolder
CharacterAdded:Connect(function(character)
local level = player.leaderstats.level
character.HumanoidRootPart.Position = Checkpoints[level.Value].Position
)

i wrote this in the forum itself so it may have some errors

2 Likes

I’ll test it out, thanks a ton for trying to help though

2 Likes

Alrighty, so it does work. BUT it only spawns me at checkpoint 3 no matter what the checkpoint value is

I’m working on finding a solution, if you have one i’d love to hear it though

1 Like

are you sure that all the checkpoints are named correctly? like 1,2,3,4,5

1 Like

Yes, they are named 1,2,3 and are in a folder in the workspace

1 Like

This is what i currently have in the function:

game:GetService("Players").PlayerAdded:Connect(function(player)
	local CheckPoint = player:WaitForChild("Checkpoint")
	player.CharacterAdded:Connect(function(character)
		local humRoot = character:WaitForChild("HumanoidRootPart")
		wait(0.01)
		humRoot.CFrame = checkpoints[CheckPoint.Value].CFrame
	end)
end)
2 Likes

Okay, so i was able to re-order the checkpoints so that the player now spawns at checkpoint one, but when i go to checkpoint 2 it updates the stats still, but doesn’t put me back there after i rejoin

1 Like

i made a script and tested and it worked disable your script and add this and see if it will work


local Players = game:GetService("Players")
local CheckPoints = workspace.CheckPoints

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder") ; leaderstats.Name = "leaderstats" ; leaderstats.Parent = player
	
	local level = Instance.new("IntValue") ; level.Name = "level" ; level.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		character:PivotTo(CheckPoints[level.Value].CFrame) -- spawn player at the checkpoint
	end)
end)

for _,CheckPoint :Part in CheckPoints:GetChildren() do
	CheckPoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = Players:GetPlayerFromCharacter(hit.Parent) -- get player
			Player.leaderstats.level.Value = CheckPoint.Name -- change the player level value
		end
	end)
end

amnot good at data stores but you can set the player data first before spawning him

3 Likes

yeah, im sorry but the same thing keeps happening! the leaderstats will update seamlessly and it saves, cause i already have a datastore in place, but it just doesnt want to teleport the player to their current checkpoint

1 Like

that is weird i will say some things that may be the problem

  • make sure you donot have checkpoints in game and that you are using parts as checkpoints
  • try adding task.delay(2) after the character added event and see if it will teleport the player
  • make sure that there isnot 2 checkpoints in the same place

i have tested with differant values when the player first join and it worked well for me you can wait for someone else to respond because i donot know

2 Likes

adding task.wait actually made it work! i have no idea why, but i aint complaining. thanks for everything, you helped so much

2 Likes

You should wrap your DataStore access with pcall() and check the return values. There are a few nuances in the meaning of the return values.

local success, data = pcall(function()
	return MyStore:GetAsync(playerKey);
end)

if success is false then access to the DataStore was unsuccessful, this could be due to any number of reasons: Roblox servers could be down, or this single call failed for whatever reason. Not checking for a failed DataStore access can result in a player being assigned new data, wiping out their current data if they already have one (MEGA data loss!!). If success is true and data is nil then no data exists for that player, and so a new store needs to be created. If success is true and data is not nil then the players data exists and is read-writable.

2 Likes

Thanks a ton for writing this, im horrible with datastores but i’ll add this now :+1:

1 Like