Touching previous stages

I’m trying to make it so when you touch a previous checkpoint the SpawnPoint value detects if it you’ve decreased by 1 or 10 depending on how far you go back which would then change the value to that specific checkpoint. I’m not sure how I’d do that so I’ve provided a script below. The SpawnPoint value acts as a checkpoint for previous stages just incase you want to help someone and don’t want to go all the way back to their level from your level and can quickly spawn there.

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.Stage.Value + 1 then --Checks if the new checkpoint is only increasing by 1.
					player.leaderstats.Stage.Value = tonumber(v.Name)
					if tonumber(v.Name) == player.hidden.SpawnPoint.Value + 1 then
						player.hidden.SpawnPoint.Value = tonumber(v.Name)
					end
				end
			end
		end)
	end
end

Just add an elseif check if the stage value is below the current and then set to the number that corresponds to that stage. It is not that much difficult than that.

1 Like

I did though it doesn’t work.

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.Stage.Value + 1 then -- Checks if the new checkpoint is only increasing by 1.
					player.leaderstats.Stage.Value = tonumber(v.Name)
					if tonumber(v.Name) == player.hidden.SpawnPoint.Value + 1 then
						player.hidden.SpawnPoint.Value = tonumber(v.Name)
					elseif tonumber(v.Name) == player.hidden.SpawnPoint.Value - 1 then
						player.hidden.SpawnPoint.Value = tonumber(v.Name)
					end
				end
			end
		end)
	end
end

One of the if checks are redundant and could be simplified.

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.Stage.Value + 1 or tonumber(v.Name) == player.leaderstats.Stage.Value - 1 then
					player.leaderstats.Stage.Value = player.leaderstats.Stage.Value < tonumber(v.Name) and tonumber(v.Name)
					player.hidden.SpawnPoint.Value = tonumber(v.Name)
				end
			end
		end)
	end
end

I assume that the leaderstats must be kept at its highest.

1 Like

When I went back one stage, it set my stage to 0 (the stage one not the SpawnPoint one) and I couldn’t go back anymore stages.

Oops, then try this instead:

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.Stage.Value + 1 then --Checks if the new checkpoint is only increasing by 1.
					player.leaderstats.Stage.Value = tonumber(v.Name)
					if tonumber(v.Name) == player.hidden.SpawnPoint.Value + 1 then
						player.hidden.SpawnPoint.Value = tonumber(v.Name)
					end
				elseif tonumber(v.Name) <= player.leaderstats.Stage.Value then
					player.hidden.SpawnPoint.Value = tonumber(v.Name)
				end
			end
		end)
	end
end

Fast edit: Just realized that you can just walk on top of any SpawnPoint and change it to that, the if statement will hinder skipped points.

1 Like