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

How do I make this have a cool-down time?

I made this script in order to spawn an object when a button is pressed and that part works, but the problem is when I tried to make a cool-down. Now, it is just always showing as ready = 1 and never 0 so there is no cool-down. Why?

local ready = script.Parent.Parent.Ready.Value
local Spawner = Vector3.new(157.326, 8.715, 26.577)
local Clicker = script.Parent 
	local Object = game.ReplicatedStorage:WaitForChild("Cardboard")
	local ObjectSpawn = script.Parent.Parent.Spawn 
	local ready = 1
	
	if ready == 1 then
		Clicker.MouseClick:Connect(function()
		ready = 0
		local CloneObject = Object:Clone()
	    CloneObject.Parent = game.Workspace 
	    CloneObject:MoveTo(Spawner)
		wait (12)
	end )
	ready = 1
end

if ready == 0 then
	script.Parent.Parent.BrickColor=BrickColor.new("Bright red")
	print("ready = 0")
else
	print ("Ready = 1")
end

1 comment

0

Add a debounce

1 answer

✓ Accepted answer

This is easy to accomplish. Here is a simple example of a debounce:

local debounce = false -- this doesn't need to be named debounce, just preference.

Clicker.MouseClick:Connect(function()
	if not debounce then

		-- by setting debounce to true, we will keep this code from executing
		-- too quickly. "if not debounce then" ensures this. It is about the
		-- same as "if debounce == false then"
		debounce = true

		-- here, you just put what you want to happen when the button
		-- is clicked
		print('Clicker was clicked!')

		wait(3) -- wait a period of time before setting debounce to false
		debounce = false

		-- now that debounce is false again, this code can execute next
		-- time Clicker.MouseClick is fired
	end
end)

Just apply this to your own code, defining the appropriate variables. Please comment if you have any questions.

1 comment

0

Thx for this I actually was about to mark this question as answered because I figured out how to use a debounce, and this is near exact to what I had. Thanks Again!

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