Spawning at First Checkpoint and not the next checkpoint

I have a checkpoint system. You spawn in at the first checkpoint with the value Enabled set to true. However when you step on another checkpoint that checkpoint is set to true and the last checkpoint is set to false. The problem is when the player dies the value for the first checkpoint resets back to true and now we have two true values for enabled. Now when the player dies and respawns they respawn at the first checkpoint instead of the second.

-- Define the starting and ending color and material
local startColor = Color3.new(1, 0.666667, 1)
local endColor = Color3.new(0, 1, 0)
local startMaterial = Enum.Material.SmoothPlastic
local endMaterial = Enum.Material.Neon.Value

local CollectionService = game:GetService("CollectionService")

local CheckPoints = CollectionService:GetTagged("CheckPoints")

-- Define the duration of the color change in seconds
local duration = 1

-- Keep track of the last checkpoint the player touched
local lastCheckpointValue = Instance.new("ObjectValue")
lastCheckpointValue.Name = "LastCheckpoint"
lastCheckpointValue.Value = nil
lastCheckpointValue.Parent = game.Players.LocalPlayer

-- Define the function to tween the part's color and material
local function tweenColor(part)
	-- Create the color and material tweens
	local colorTween = game:GetService("TweenService"):Create(part, TweenInfo.new(duration), {Color = endColor})
	local materialTween = game:GetService("TweenService"):Create(part, TweenInfo.new(duration), {Material = endMaterial})

	-- Play the tweens in parallel
	colorTween:Play()
	materialTween:Play()
end

-- Set up the Touched event for each checkpoint
for i, checkpoint in ipairs(CheckPoints) do
	checkpoint.Touched:Connect(function(hit)
		if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
			-- Disable the last checkpoint the player touched
			local lastCheckpoint = lastCheckpointValue.Value
			if lastCheckpoint then
				lastCheckpoint.Enabled = false
			end

			-- Set the new checkpoint as the last checkpoint the player touched
			lastCheckpointValue.Value = checkpoint

			-- Enable the new checkpoint
			checkpoint.Enabled = true

			-- Tween the checkpoint's color and material
			tweenColor(checkpoint)
		end
	end)
end

-- Set the player's respawn location based on the last checkpoint they touched
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	character.Humanoid.Died:connect(function()
		if character.Humanoid.Health == 0 then
			local lastCheckpoint = lastCheckpointValue.Value
			if lastCheckpoint and character:FindFirstChild("Humanoid") then
				local spawnLocation = lastCheckpoint.SpawnLocation
				character.HumanoidRootPart.CFrame = spawnLocation.CFrame
			end
		end
	end)
end)

The problem is from the respawning part,
Basically you’re teleporting the player after he dies, you should only use CharacterAdded as it detects when the player’s character has respawned and also it is recommended to use :WaitForChild()

Replace your last part with this:

	game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
		local lastCheckpoint = lastCheckpointValue.Value
		if lastCheckpoint and character:WaitForChild("HumanoidRootPart") then
			local spawnLocation = lastCheckpoint.SpawnLocation
			character.HumanoidRootPart.CFrame = spawnLocation.CFrame
		end
	end)
1 Like

Sadly it did not work Im still spawning in at the first spawn location :smiling_face_with_tear:

Try this for your CharacterAdded:

game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	repeat task.wait() until character.Parent ~= nil
	
	local lastCheckpoint = lastCheckpointValue.Value
	if lastCheckpoint and character:FindFirstChild("Humanoid") then
		local spawnLocation = lastCheckpoint.SpawnLocation
		character.HumanoidRootPart.CFrame = spawnLocation.CFrame + Vector3.new(0, 2, 0)
	end
end)
1 Like

unfortunately that did not work either

What type of object are you using for the CheckPoints? I used a Part but you are actually setting checkpoint.Enabled which is not a property of part so I may have done it slightly different.

1 Like

Im using a spawn location part

Okay SpawnLocations did make a little difference. I just had to make sure the SpawnLocations were all disabled before starting the game. I think this line threw me off:

local spawnLocation = lastCheckpoint.SpawnLocation

That would be having a SpawnLocation as a child of a SpawnLocation which is why I made Parts. When I did this I changed that to:

local spawnLocation = lastCheckpoint

Since I just made the Checkpoints SpawnLocations. Hope this helps.

1 Like

Bro you dont have any idea how much you helped me. I cannot thank you enough; literally been losing my mind over this checkpoint system thanks again for your insightful comments. I’m going to mark your comment as a solution

I completely understand the feeling. Glad it helped.

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