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 6 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()

01print("Button was clicked.")
02script.Parent.Active = false
03script.Parent.Selectable = false
04 
05game.ReplicatedStorage.STOPbutton:FireServer()
06 
07for i = 10, 0, -1 do
08    script.Parent.TextLabel.Text = i
09    wait(1)
10end
11 
12for i = 20, 1, -1 do
13    script.Parent.TextLabel.TextColor3 = Color3.new(0,0,0)
14    script.Parent.TextLabel.Text = i
15    wait(1)
16end
17script.Parent.TextLabel.Text = "STOP!"
18script.Parent.Active = true
19script.Parent.Selectable = true
20script.Parent.TextLabel.TextColor3 = Color3.new(255,255,255)

end)

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

1 answer

Log in to vote
1
Answered by 6 years ago

Like that?

01script.Parent.MouseButton1Click:Connect(function()
02    if script.Parent.Active then
03        print("Button was clicked.")
04        script.Parent.Active = false
05        script.Parent.Selectable = false
06 
07        game.ReplicatedStorage.STOPbutton:FireServer()
08 
09        for i = 10, 0, -1 do
10            script.Parent.TextLabel.Text = i
11            wait(1)
12        end
13 
14        for i = 20, 1, -1 do
15            script.Parent.TextLabel.TextColor3 = Color3.new(0,0,0)
View all 25 lines...
1
This works. Thank you!! AceGamer247 7 — 6y
Ad

Answer this question