Touch event messing with my gui script

So I basically made a checkpoint script, when ever you touch one of the stages then that will be your checkpoint if you meet the requirements, And I also made a gui where you can use 2 buttons to go back, and forth between the stages that you have unlocked, the only issue rn is that when ever you go backwards the touch event prob fires at the same thing causing you to be set back to the stage you just teleported from some times, it is just kind of buggy

I have tried to set a debounce so that the touch event couldn’t continue when the code that is handling the gui stuff is being run, but that also doesn’t seem to work, I just don’t know what the solution could be rn, so if anybody could take the time to help then that would be amazing.

local Collection = game:GetService("CollectionService")
local Rep = game:GetService("ReplicatedStorage")

for _, Stage in pairs(Collection:GetTagged("Stage")) do
	Stage.Touched:Connect(function(Hit)
		
		local humanoid = Hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			local PlayerStage = Player:WaitForChild("leaderstats").Stage
			local HSU = Player:WaitForChild("HighestStageUnlocked")
			local NewStage = Stage.Name
			
			if PlayerStage.Value > tonumber(NewStage) then
				return
			end
			
			if PlayerStage.Value ~= tonumber(NewStage) -1 then
				return
			end
			PlayerStage.Value =  NewStage
			if tonumber(NewStage) > HSU.Value then
				HSU.Value = NewStage
			end
			Rep.Gui:FireClient(Player, NewStage)
		end
	end)
end


Rep.GuiStageChange.OnServerInvoke = function(_,Player, StageJump)
	local leaderstats = Player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	local HSU = Player:WaitForChild("HighestStageUnlocked")
	if Stage.Value + StageJump <= HSU.Value then
		if Stage.Value + StageJump > 0 then
			
			Stage.Value = Stage.Value + StageJump
			local CurrentStage = game.Workspace.Game.Stages:FindFirstChild(Stage.Value)
			Player.Character.HumanoidRootPart.CFrame = CurrentStage.CFrame + Vector3.new(0,1,0)
			return Stage.Value
		end
	end
end
2 Likes

What is the Name of the stage? Is it a string? and you’re saying tonumber(“String”)?

The name of the stage is a number, and I am pretty sure I used “tonumber” because it was seen as a string, and not an actual number.

Is this because you’re teleporting directly onto another part with a touch event?

I think that what is happening is that when you teleport when you click on the backwards arrow in my gui menu is some sometimes also fires the touch event at the same time what causes your stage to be set back to the stage that you just teleported from, but I am not totally sure if that’s true, and I don’t know how to fix it atm.