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

Why is my debounce not resetting when the script is finished?

Asked by 5 years ago

So, I have a script that gets the value of an int value, and runs code depending on what the value is (for rn it just prints what the value is) anyways, I added a debounce the debounce is set to false at first (needs to be set to false for code to run) then set to true, then back to false. well, whenever debounce is set to false, I made it print("debounce is false"), and same for when its set to true. anyways, whenever I touch the brick (this is all inside a function btw), it does, as it should, print debounce is true, waits 20 seconds, then prints debounce is false but on the second go-around that you touch the part, it prints debounce is false, and never prints debounce is true and I have no clue why any help would be appreciated PLEASE DO NOT SPOON FEED ME.

debounce = false
game.ReplicatedStorage.GetPowerup.OnServerEvent:connect(function(plr)
    if debounce == false then
        debounce = true
        print("debounce true")
    if script.Parent.tier.Value == 1 then
        print("One")
    elseif script.Parent.tier.Value == 2 then
        print("Two")
    elseif script.Parent.tier.Value == 3 then
        print("Three")
    elseif script.Parent.tier.Value == 4 then
        print("Four") 
    elseif script.Parent.tier.Value == 5 then
        print("Five")

    end
    end
end)
wait(20)
print("debounce is false")
        debounce = false
0
Can you show the whole code including the function? xxXTimeXxx 101 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I believe you should be changing debounce inside the event.

local debounce = false -- Local variables are easier to access
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Powerup = ReplicatedStorage:WaitForChild("GetPowerup")

Powerup.OnServerEvent:Connect(function(plr)
    -- Switch to :Connect, :connect is deprecated 
    if not debounce then -- No need for == false
        debounce = true
        print("debounce true")

        if script.Parent.tier.Value == 1 then
            print("One")
        elseif script.Parent.tier.Value == 2 then
            print("Two")
        elseif script.Parent.tier.Value == 3 then
            print("Three")
        elseif script.Parent.tier.Value == 4 then
            print("Four") 
        elseif script.Parent.tier.Value == 5 then
            print("Five")
        end

        wait(20)
        debounce = false
        print("debounce is false")
    end
end)


I also tidied up your code a bit, you're welcome.

0
It worked, but why did it fully loop the debounce the first time but not the second? OldSoul04 13 — 5y
Ad

Answer this question