Going back to previous checkpoints

I’m having an issue where I’m not sure how I’d make it so that when you touch a previous checkpoint, that the value for SpawnPoint would change to that checkpoint number. Here’s the script that recognizes when you touch a checkpoint. Let me know if you need more information.

The script:

local checkpoints = workspace:WaitForChild("Checkpoints")


for i,v in pairs(checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
				if tonumber(v.Name) > player.leaderstats.Checkpoint.Value then
					player.leaderstats.Checkpoint.Value = tonumber(v.Name)
					if tonumber(v.Name) > player.leaderstats.SpawnPoint.Value then
						player.leaderstats.SpawnPoint.Value = tonumber(v.Name)

					
					end
					
				end
				
			end
		end)
	end
end


this line makes it only change when they touch a bigger checkpoint number so if you want to change checkpoint value if they touch to previous checkpoint you can just remove this if statement

Edit:

if tonumber(v.Name) > player.leaderstats.SpawnPoint.Value then

and this line i guess but I don’t know what are you using SpawnPoint value for so i just said Checkpoint value

Nope, that breaks the whole thing. The SpawnPoint is an IntValue that keeps track when you touch a previous checkpoint, that you respawn (I already got that covered) there instead of at your farthest checkpoint. The checkpoint just indicates how far you’ve gotten too.

what errors did you get when you remove thaf if statement?

Didn’t see that there was an error with wrapping the function, my bad. We’re on the right track, however I only want this to apply for the SpawnPoint only, not the Checkpoint. It changes both values when you go back instead of just SpawnPoint which is the goal.

can you show the current updated script?

local checkpoints = workspace:WaitForChild("Checkpoints")


for i,v in pairs(checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
					player.leaderstats.Checkpoint.Value = tonumber(v.Name)
					if tonumber(v.Name) > player.leaderstats.SpawnPoint.Value then
					player.leaderstats.SpawnPoint.Value = tonumber(v.Name)
					
				end
			end
		end)
	end
end


It works, problem is that it updates both Checkpoint and SpawnPoint when you go back to a previous stage, but instead when you go back the value that should be updated is SpawnPoint.

so you mean you only want to change SpawnPoint value?

Well I want to change both, the CheckPoint should increase when you go farther and farther. Instead for SpawnPoint when you go back to a previous level it should decrease.

then you should remove this if statement too so it will change both values

Won’t that do the exact same thing? Checkpoint should increase when you progress and it’ll represent how far you’ve gotten too, so that will increase as you progress. However, SpawnPoint should decrease when you go back a few checkpoints.

ah sorry my bad i thought you wanted to change both values when you touch previous checkpoint

updated script should look like this:

local checkpoints = workspace:WaitForChild("Checkpoints")


for i,v in pairs(checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
				if tonumber(v.Name) > player.leaderstats.Checkpoint.Value then
					player.leaderstats.Checkpoint.Value = tonumber(v.Name)
					
				end
				player.leaderstats.SpawnPoint.Value = tonumber(v.Name)

			end
		end)
	end
end

that will update only SpawnPoint value when you touch to previous checkpoint

1 Like

Sometimes It wont work whatever reason, it seems to only work for the first few minutes. :pensive:

I know its a different issue, however I think it’d be appropriate to put it under this thread. Here’s the respawn script.

	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.leaderstats.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,2,0)
	end)
	

sorry for late reply.

CharacterAdded fires whenever your character respawns so you don’t need repeat loop there and i guess you already have a player variable and you’re creating another one :thinking:

and you have a checkpoint at the start which will make SpawnPoint value 0 when you reset because you’re touching it whenever you die

Oh, I see what you’re getting at. Let me see if I can fix this real quick and I’ll get back to you soon.