Weird Checkpoint Behavior

So, I made a checkpoint system for my obby, but the issue is quite complicated to explain, so I’ll put on a hypothetic situation:

  • Imagine Little Timmy died 2 times at checkpoint 1, but then he spawns back at the checkpoint 0.

The first time you die at the other checkpoint, you respawn there as intended, but when you die for the second time, the player will spawn back at the beginning.

CODE:

local CollectService = game:GetService("CollectionService")

local checkpoints = CollectService:GetTagged("Checkpoint")

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local checkpoint = Instance.new("NumberValue")
	checkpoint.Name = "Checkpoint"
	checkpoint.Parent = leaderstats
	
	player.CharacterAdded:Wait()
	player.Character:WaitForChild("Humanoid").Died:Connect(function()
		player.CharacterAdded:Wait()
		
		local root = player.Character:WaitForChild("HumanoidRootPart")
		
		for _, v in ipairs(checkpoints) do
			if v:IsA("BasePart") then
				if v.Name == "Checkpoint"..checkpoint.Value then
					root.CFrame = v.CFrame
				end
			end
		end
	end)
end)

for _, v in ipairs(checkpoints) do
	if v:IsA("BasePart") and v:FindFirstChild("Checkpoint") then
		v.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			if player then
				player:WaitForChild("leaderstats"):WaitForChild("Checkpoint").Value = v.Checkpoint.Value
			end
		end)
	end
end

FOOTAGE:
robloxapp-20240227-1005388.wmv (2.3 MB)

Your Died function only works for the first character. Each subsequent character (after player dies) will have a new humanoid, therefore your function, which is connected to the first humanoid, will not run.

This will cause player to remain at the spawn location.

To fix you need to connect your function to CharacterAdded rather than Humanoid.Died.,EDIT: while Humanoid.Died is inside this function, making sure it is always re-connected to the new humanoid.

--BONUS TIP:
--I always define the "OnPlayerAdded" function first. There is a slight chance that during studio testing player spawns before this script runs, breaking the test and causing confusion.

local function OnPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local checkpoint = Instance.new("NumberValue")
	checkpoint.Name = "Checkpoint"
	checkpoint.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)

		character:WaitForChild("Humanoid").Died:Connect(function()
			local root = character:WaitForChild("HumanoidRootPart")
		
			for _, v in ipairs(checkpoints) do
				if v:IsA("BasePart") then
					if v.Name == "Checkpoint"..checkpoint.Value then
						root.CFrame = v.CFrame
					end
				end
			end
		end)
	end)
end

--run function for every player that is already in the game
for _, plr in pairs(Players:GetPlayers()) do
	OnPlayerAdded(plr)
end
--connect here
Players.PlayerAdded:Connect(OnPlayerAdded)

Hope this helps.

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