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

How to disable a button until it finishes running a function?

Asked by 5 years ago

I am making an imagebutton in the player's ScreenGui that fires a remote event for 10 seconds, then has a 20 second cooldown time. When I click the button once, it works fine, but if I click the button multiple times, the function and the timer I made restart as well. Is there some way to prevent the button from receiving user input while its function is still running? Here is my code to give you an idea of what I mean. Inside my imagebutton is a textlabel and a local script. The following code is from the localscript.

script.Parent.MouseButton1Click:Connect(function()

print("Button was clicked.")
script.Parent.Active = false
script.Parent.Selectable = false

game.ReplicatedStorage.STOPbutton:FireServer()

for i = 10, 0, -1 do
    script.Parent.TextLabel.Text = i
    wait(1)
end

for i = 20, 1, -1 do
    script.Parent.TextLabel.TextColor3 = Color3.new(0,0,0)
    script.Parent.TextLabel.Text = i
    wait(1)
end
script.Parent.TextLabel.Text = "STOP!"
script.Parent.Active = true
script.Parent.Selectable = true
script.Parent.TextLabel.TextColor3 = Color3.new(255,255,255)

end)

2
use a debounce User#24403 69 — 5y
0
debounce is the way to go LoganboyInCO 150 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Like that?

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.Active then
        print("Button was clicked.")
        script.Parent.Active = false
        script.Parent.Selectable = false

        game.ReplicatedStorage.STOPbutton:FireServer()

        for i = 10, 0, -1 do
            script.Parent.TextLabel.Text = i
            wait(1)
        end

        for i = 20, 1, -1 do
            script.Parent.TextLabel.TextColor3 = Color3.new(0,0,0)
            script.Parent.TextLabel.Text = i
            wait(1)
        end

        script.Parent.TextLabel.Text = "STOP!"
        script.Parent.Active = true
        script.Parent.Selectable = true
        script.Parent.TextLabel.TextColor3 = Color3.new(255,255,255)
    end
end)
1
This works. Thank you!! AceGamer247 7 — 5y
Ad

Answer this question