Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My debounce not work? Work only my first function

Hi guys, sorry for bad english.

This script work only the first time, after stop working, like an infinite Debounce

This is only a little part of my script. Work only the first function (?Maybe because i use the same debounce?) Is better if i use the _G. debounce?

local Phere2 = workspace.StartPhere['Complimenti!']:WaitForChild("2")
local Phere3 = workspace.StartPhere['Complimenti!']:WaitForChild("3")
local Phere4 = workspace.StartPhere['Complimenti!']:WaitForChild("4")

local debounce = false

local function OnTouch2()
	if not debounce then
	debounce = true
		if player.leaderstats.PhereStage.Value == 1 then
		ShowMessage:Fire("Complimenti! Hai superato la fase 1 della Obby di Phere!", true)
		sound:Play()
		wait (.5)
		debounce = false
		end
	end
end
Phere2.Touched:Connect(OnTouch2)

local function OnTouch3()
	if not debounce then
	debounce = true
		if player.leaderstats.PhereStage.Value == 2 then
		ShowMessage:Fire("Complimenti! Hai superato la fase 2 della Obby di Phere!", true)
		sound:Play()
		wait (.5)
		debounce = false
		end
	end
end
Phere3.Touched:Connect(OnTouch3)

local function OnTouch4()
	if not debounce then
	debounce = true
		if player.leaderstats.PhereStage.Value == 3 then
		ShowMessage:Fire("Complimenti! Hai superato la fase 3 della Obby di Phere!", true)
		sound:Play()
		wait (.5)
		debounce = false
		end
	end
end
Phere4.Touched:Connect(OnTouch4)

EDIT: "You not change the PhereStage.Value, for this not work!"

ERROR, i change it with a script inside the Object

function Pherestage(hit)
if hit.Parent ~= nil then
local player = game.Players:playerFromCharacter(hit.Parent)
	if player ~= nil then
		if player.leaderstats.PhereStage.Value == script.Parent.Name - 1 then
		local plr = hit.Parent:FindFirstChild("Humanoid")
			if plr ~= nil then
				wait(0.2)
				player.leaderstats.PhereStage.Value = script.Parent.Name
				end 
			end 
		end 
	end
 end
end

script.Parent.Touched:connect(Pherestage)

1 comment

0

Also _G won't do anything as it just makes it a global variable.. and you have your debounce variable anyway at the top of the script.

1 answer

✓ Accepted answer

You shouldn't reset the debounce inside of a conditional, as if the conditional ever fails (which I expect it is in this case) then your debounce will be stuck as true forever.

In all three OnTouch functions, move the debounce reset outside of the conditional like this:

local function OnTouch2()
	if not debounce then
		debounce = true
		if player.leaderstats.PhereStage.Value == 1 then
			ShowMessage:Fire("Complimenti! Hai superato la fase 1 della Obby di Phere!", true)
			sound:Play()
		end

		-- Move it to here so that it is always reset, regardless of the value of PhereStage
		wait (.5)
		debounce = false
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.