How do i make this only accept one click?

Hello. I am trying to make a model that when you click, something happens. Before I tell you what I need help with, I am not asking you for how to make the model. I noticed that if I click multiple times, It makes 2 outputs Kinda like if you press a button 2 or more times in Minecraft while the button is connected to many repeaters lol :wink: and thats not what I want.

Video robloxapp-20200303-1637136.wmv (575.7 KB)

script

local M3 = game.Workspace.Model3 --to model3
local M3CD = M3.Lift.Model.CD --to the click detector block
---------------------------------------------------------------------
local function TK() --3 ticks
	script.Tick:Play()
	wait(1)
end
---------------------------------------------------------------------

M3CD.ClickDetector.MouseClick:Connect(function() --this is how ClickDetector works
	script.Click:Play()
	--adding something here later
	wait(1)
	TK()
	TK()
	TK()
	
	print("Model3 Lift has started up") --lift has begun
	
	--adding something here to
	wait(1)
	print("Model3 Lift task completed reverting back to normal") --lift is returning
	script.Ding:Play()
	--same here later
end)

script properties
Capture4
Highlighted stuff important stuff. Behind model3 is workspace
I want it so that way it can only be accepted once and not multiple times like what you seen in the video.

2 Likes

You need to use a debounce. Debounce makes it so that you control when the button can be fired. To create a debounce you need an if statement and a variable to tell the if statement when it’s ready to fire. When applied to your script, it would look like this:

local M3 = game.Workspace.Model3 --to model3

local M3CD = M3.Lift.Model.CD --to the click detector block

local ready = true

local function TK() --3 ticks

script.Tick:Play()

wait(1)

end

M3CD.ClickDetector.MouseClick:Connect(function() --this is how ClickDetector works

if not ready then return end

ready = false

script.Click:Play()


wait(1)

TK()

TK()

TK()

print("Model3 Lift has started up") --lift has begun

--adding something here to

wait(1)

print("Model3 Lift task completed reverting back to normal") --lift is returning

script.Ding:Play()

ready = true

--same here later

end)

When I pasted the script from studio, the formatting got messed up for some reason.

As you can see, I added a variable called ready to tell the script when the button can be activated again. After the script is ran, we made the ready variable equal to false. I also added an if statement. The if statement’s job is to check if it’s ready. If it’s not ready it returns so that the script doesn’t run. At the end, the ready variable is set to true again so that the if statement knows that it can run again

2 Likes

If I read your post correctly, we can use the ‘debounce’ method.

Basically, ‘debounce’ is the process of setting/checking a variable before we execute any code in an function. We set it to a value, commonly a bool like true. Then, we add a condition statement checking to see if it isn’t set to true, and if so, we execute some code. Here’s how that’d look in your current code:

local debounce = false
M3CD.ClickDetector.MouseClick:Connect(function() --this is how ClickDetector works
if debounce then return end --End function if debounce is set to true
        debounce = true --Set debounce to true so the function doesn't run rapidly
	script.Click:Play()
	--adding something here later
	wait(1)
	TK()
	TK()
	TK()
	
	print("Model3 Lift has started up") --lift has begun
	
	--adding something here to
	wait(1)
	print("Model3 Lift task completed reverting back to normal") --lift is returning
	script.Ding:Play()
        --//debounce = false Commented out if you don't want this function to ever run again.
	--same here later
end)
1 Like