Checkpoint script not working?

Hi guys! I’m currently working on a game which needs a checkpoint system. I tried a tutorial as I know basically nothing about scripting. Although this script shows stages on the player list, it doesn’t change the number or save your checkpoint so when you die you go right back to the start.
This is the script:
local checpoints = workspace:WaitForChild(“Checkpoints”)

game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = plr

local v =Instance.new("IntValue")
v.Name = "Stage"
v.Parent = leaderstats

plr.CharacterAdded:Connect(function(char)
	local hum = char:WaitForChild("Humanoid")
	wait()
	char:WaitForChild("HumanoidRootPart").CFrame = workspace.Checkpoints:FindFirstChild("Stage0").CFrame
	
	hum.Touched:Connect(function(hit)
		if hit.Parent == checkpoints then
			if tonumber(hit.Name) == v.Value +1 then
				v.Value = v.Value +1
			end
		end
	end)
end)

end)

Any help would be appreciated.

2 Likes

What issue are you having with this?

You are calling the touched event when the “humanoid” is touched. This is not a function I’m pretty sure. Try character.LeftLeg.Touched instead.

The reason why you go back to the start is because of this line. It only teleports to Stage0.

You should instead do:

char:WaitForChild("HumanoidRootPart").CFrame = workspace.Checkpoints:FindFirstChild("Stage"..v.Value).CFrame

And the reason why it doesn’t change the number because for example, Stage0. That can’t be transfered to a number so the if statement won’t work. Use string.sub to get the number.

@MegaFireball0 There’s an event for that in Humanoid.

1 Like

Ok, I put the first part in and I think I done it right. However, when I put string.sub in, I no longer spawn in the lobby and instead I spawn in the air and fall to my death. I probably put it in the wrong place or something as I put it below the char:WaitForChild line.

Did you replace this with string.sub or you put it somewhere else? Also, you should set the v.Value to the first spawn or the script won’t know where to teleport.

1 Like

No, I put it somewhere else, should it replace this entire line?

if tonumber(hit.Name) == v.Value +1 then

Also do I just change this line so that it says Stage0 instead of v.Value + 1

v.Value = v.Value +1

Sorry about this. It’s probably really straight forward but this is my first attempt at scripting so I still don’t understand it.

Replace that line with this:

if tonumber(string.sub(hit.Name, 6, 6)) == v.Value + 1 then
1 Like

try this


local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function (plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local v = Instance.new("IntValue")
	v.Name = "Stage"
	v.Value = 0
	v.Parent = leaderstats
	
	plr.CharacterAdded:Connect(function (char)
		local hum = char:WaitForChild("Humanoid")
		local hrp = char:WaitForChild("HumanoidRootPart")
		wait()
		if hum.Health > 0 then
			hrp.CFrame = CFrame.new(workspace.Checkpoints:FindFirstChild(
				"Stage" .. tostring(v.Value)).CFrame.Position + Vector3.new(0, 5, 0))
		end
		
		hum.Touched:Connect(function(hit)
			if hit.Parent == workspace.Checkpoints then
				if hit.Name == "Stage" .. tostring(v.Value + 1) then
					v.Value = v.Value + 1
				end
			end
		end)
	end)

end)

I’m assuming you have this
image

4 Likes

Thank you so much. That works perfectly from my quick test.

Sorry, I made a small correction. Now it’s okay.