Problem with checkpoint system

sometimes when the player spawns they spawn at the spawn location instead of the checkpoint, i tried deleting the spawn location but it still does that

script:

local checkpoints = workspace:WaitForChild(“Checkpoints”)

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 = 0
stage.Parent = leaderstats

player.CharacterAdded:Connect(function(char)
	local hum = char:WaitForChild("Humanoid")
	wait()
	char:MoveTo(checkpoints[stage.Value].Position)

	hum.Touched:Connect(function(hit)
		if hit.Parent == checkpoints then
			if tonumber(hit.Name) == stage.Value + 1 then
				stage.Value = stage.Value + 1
			end
		end
	end)
end)

end)

Are you trying to make the Character walk to that point, or teleport?

teleport to that point once they respawn

Just change the CFrame or Position of the Character.HumanoidRootPart, using the :MoveTo() function is used for making the character walk to a given position.

its already used here as i see, its not my script but they all share the same error

Do you have a checkpoint named “0” because if you don’t it might mess up the script.

player.CharacterAdded:Connect(function(char)
        repeat task.wait(0.1) until char:FindFirstChild("HumanoidRootPart") and char:FindFirstChildWhichIsA("Humanoid")
	local hum = char:FindFirstChildWhichIsA("Humanoid")
	char:SetPrimaryPartCFrame(checkpoints[stage.Value].CFrame)

	-- do touching stuff here (YOU CANT DETECT .TOUCHED FOR HUMANOIDS)
end)

I am unable to test but this should work.

yep i have a 0 checkpoint, thats why the value is set to 0

Sometimes the player will join before the script connects the event. You may need to loop over player list and call your function.

Check that page. GetPlayers has two code samples, #2 may be what you need.

you can simply use spawnlocations and use set the spawnlocation to the checkpoint the player is on.

Maybe try your code like this so there is no delay in setting up the playeradded function (there is a chance that it won’t catch the first player joining the came you can detect this with adding prints to test your code)

game.Players.PlayerAdded:Connect(function(player)
	print('PlayerJoined ', player)  -- for testing
	local leaderstats = Instance.new(“Folder”)
	leaderstats.Name = “leaderstats”
	leaderstats.Parent = player

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 0
	stage.Parent = leaderstats

	player.CharacterAdded:Connect(function(char)
		local checkpoints = workspace:WaitForChild(“Checkpoints”)  -- moved this here so it won't delay the player added function
		local hum = char:WaitForChild("Humanoid")
		wait()
		
		char:MoveTo(checkpoints[stage.Value].Position)
		
		print('CharacterMoved ', char)  -- for testing
		
		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)
	end)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.