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 4 years ago
Edited 4 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?

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)
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 — 4y

1 answer

Log in to vote
3
Answered by 4 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:

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
Ad

Answer this question