Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My debounce not work? Work only my first function

Asked by 5 years ago
Edited 5 years ago

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?

01local Phere2 = workspace.StartPhere['Complimenti!']:WaitForChild("2")
02local Phere3 = workspace.StartPhere['Complimenti!']:WaitForChild("3")
03local Phere4 = workspace.StartPhere['Complimenti!']:WaitForChild("4")
04 
05local debounce = false
06 
07local function OnTouch2()
08    if not debounce then
09    debounce = true
10        if player.leaderstats.PhereStage.Value == 1 then
11        ShowMessage:Fire("Complimenti! Hai superato la fase 1 della Obby di Phere!", true)
12        sound:Play()
13        wait (.5)
14        debounce = false
15        end
View all 44 lines...

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

ERROR, i change it with a script inside the Object

01function Pherestage(hit)
02if hit.Parent ~= nil then
03local player = game.Players:playerFromCharacter(hit.Parent)
04    if player ~= nil then
05        if player.leaderstats.PhereStage.Value == script.Parent.Name - 1 then
06        local plr = hit.Parent:FindFirstChild("Humanoid")
07            if plr ~= nil then
08                wait(0.2)
09                player.leaderstats.PhereStage.Value = script.Parent.Name
10                end
11            end
12        end
13    end
14 end
15end
16 
17script.Parent.Touched:connect(Pherestage)
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. 123nabilben123 499 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago

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:

01local function OnTouch2()
02    if not debounce then
03        debounce = true
04        if player.leaderstats.PhereStage.Value == 1 then
05            ShowMessage:Fire("Complimenti! Hai superato la fase 1 della Obby di Phere!", true)
06            sound:Play()
07        end
08 
09        -- Move it to here so that it is always reset, regardless of the value of PhereStage
10        wait (.5)
11        debounce = false
12    end
13end
Ad

Answer this question