Checkpoint not working

Hello, so this might be a basic question… and I haven’t scripted in a while, but I’m confused why this checkpoints script is not working.

local players = game.Players

players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local hrp = c:WaitForChild("HumanoidRootPart", 4)
		
		if hrp ~= nil then
			local stage = p.leaderstats.Stage.Value
			
			if stage ~= nil then
				print("e")
				print(game.Workspace.Map.Checkpoints:FindFirstChild(tostring(stage)).CFrame)
				p.Character.HumanoidRootPart.CFrame = game.Workspace.Map.Checkpoints:FindFirstChild(tostring(stage)).CFrame or game.Workspace.Map.Checkpoints:FindFirstChild("1").CFrame
			end
		end
		
	end)
end)

Is the print printing out nil, or is it printing out the wrong checkpoint? How are you changing the Stage value, is it through a script or through the properties tab?

The print is printing out correctly, however it won’t set the position of the player.

Then the only thing I can think that could be the problem is the “or” statement. Maybe try this:

local players = game.Players

players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local hrp = c:WaitForChild("HumanoidRootPart", 4)
		if hrp ~= nil then
			local stage = p.leaderstats.Stage.Value
			if stage ~= nil then
				local ChosenCheckpoint = game.Workspace.Map.Checkpoints:FindFirstChild(tostring(stage)) or game.Workspace.Map.Checkpoints:FindFirstChild("1")
                p.Character.HumanoidRootPart.CFrame = ChosenCheckpoint.CFrame
			end
		end
		
	end)
end)

I tried it and it still doesn’t work, I’m confused why it won’t set the position.

the problem is you are putting the CFrame in the FindFirstChild Function you need to put it out of the parentheses.

It should look like:

FindFistChild(tostring(stage)).CFrame

That is the print statement for testing, and it’s in the print statement, so it prints the position for testing… I think you got a little bit confused.

That’s the print statement-

print(game.Workspace.Map.Checkpoints:FindFirstChild(tostring(stage)).CFrame)
print() --Empty print
print(workspace.Map.Checkpoints:FindFirstChild()) --Print with findfirstchild

Now I see I got confused for some reason I thought it said FindFirstChild(tostring(stage).CFrame)

In my code, if you print out “ChosenCheckpoint” does it print the right checkpoint?

It gets the checkpoint part, and then gets the cframe so it can print it out.

Yeah I think so, it seems to print out the correct positions.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local hrp = c:WaitForChild("HumanoidRootPart")
        local stage = p:WaitForChild("leaderstats").Stage
			
        local CurrentStage = workspace.Map.Checkpoints:FindFirstChild(tostring(stage.Value))
        print(CurrentStage)
        if CurrentStage then
			hrp.CFrame = workspace.Map.Checkpoints:FindFirstChild(tostring(stage)).CFrame
            print("Teleporting player to last saved stage")
        else
            hrp.CFrame = workspace.Map.Checkpoints:FindFirstChild("1").CFrame	
            print("Teleporting player to the beginning")
        end
    end)
end)

Try this perhaps?