Debounce Not Working If Player Leaves After Hitting Part

While testing the game for bugs, this script’s debounce remains as false if the player leaves the game right after hitting the part.

Script |

local Spawns = game.Workspace.Lobby.Map:FindFirstChild("Spawns"):GetChildren()
local Teleports = game.Workspace.Stages:FindFirstChild("Teleports")

local Debounce = false

script.Parent.Touched:Connect(function(Hit)

	if Debounce then return end

	Debounce = true

	local PlayerStats = game.Players:GetPlayerFromCharacter(Hit.Parent)

	if Hit.Parent:FindFirstChild("Humanoid") then

		local Player = Hit.Parent

		Player.Torso.CFrame = Spawns[math.random(1, #Spawns)].CFrame
		PlayerStats.leaderstats.XP.Value = PlayerStats.leaderstats.XP.Value + 90

		if PlayerStats.DoubleXP.Value == true then
			PlayerStats.leaderstats.XP.Value = PlayerStats.leaderstats.XP.Value + 90
		end

		wait(1)
		Debounce = false
	end
end)
1 Like

Are there any errors on the Output?

While attempting to debug it no errors have shown up in the output and I have also tried printing stuff in the output after each line is executed and it works fine. Though for example, say I left the game right when I hit the part, then the debounce remains false.

You could try setting the Debounce outside the conditional check perhaps?

local Spawns = game.Workspace.Lobby.Map:FindFirstChild("Spawns"):GetChildren()
local Teleports = game.Workspace.Stages:FindFirstChild("Teleports")

local Debounce = false

script.Parent.Touched:Connect(function(Hit)

	if Debounce then return end

	Debounce = true

	local PlayerStats = game.Players:GetPlayerFromCharacter(Hit.Parent)

	if Hit.Parent:FindFirstChild("Humanoid") then

		local Player = Hit.Parent

		Player.Torso.CFrame = Spawns[math.random(1, #Spawns)].CFrame
		PlayerStats.leaderstats.XP.Value = PlayerStats.leaderstats.XP.Value + 90

		if PlayerStats.DoubleXP.Value == true then
			PlayerStats.leaderstats.XP.Value = PlayerStats.leaderstats.XP.Value + 90
		end
	end
	wait(1)
	Debounce = false
end)
1 Like

Thanks for replying, I’ll try this script. I’ll get back to you right after I test it with someone, which will take a few minutes.

Thanks for the solution, the debounce outside of the conditional has patched the bug.

1 Like