Checkpoints Not Working

For some reason, the checkpoints only work some of the time. Sometimes the player is moved to the checkpoint, sometimes they aren’t. It seems to happen at random.

The Code:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local checkpoint = script.Parent

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		
		if player.Team == game.Teams.Runners then	
			game.ReplicatedStorage.Checkpoint:FireClient(player, checkpoint)
			
			local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
			if not checkpointData then
				checkpointData = Instance.new("Folder")
				checkpointData.Name = "CheckpointData"
				checkpointData.Parent = ServerStorage
			end
			
			local userIdString = tostring(player.UserId)
			local checkpointValue = checkpointData:FindFirstChild(userIdString)
			if not checkpointValue then
				checkpointValue = Instance.new("ObjectValue")
				checkpointValue.Name = userIdString
				checkpointValue.Parent = checkpointData
				
				player.CharacterAdded:connect(function(character)
					wait()
					local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
					if player:WaitForChild("Lives").Value ~= 6 and player:WaitForChild("Lives").Value ~= 0 then
						character:MoveTo(storedCheckpoint.Position)
					end
				end)
			end
			
			checkpointValue.Value = checkpoint
		end
	end
end

checkpoint.Touched:Connect(onTouched)
2 Likes