Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comments

2

use a debounce

0

debounce is the way to go

1 answer

✓ Accepted answer

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 comment

1

This works. Thank you!!

This discussion is preserved as it was. New answers, comments, and votes are closed.