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

Debounce not working very well?

I have added a debounce but it doesnt work and the output and script is rank 5 separate times

If you could help that'd be great

local Config1 = game.Workspace.Config
local Config = require(Config1)
local buttonPressed = false

script.Parent.Parent.Parent.Submit.MouseButton1Click:Connect(function()
		if buttonPressed == false then
        			buttonPressed = true 
			script.Value.Value = script.Value.Value + 1
			if script.Value.Value == Config.questionAmount + 1 then
					local Value = script.Value.Value
					game.ReplicatedStorage.Check:FireServer(Value)
				else	
					
					script.Parent.Parent.Parent.Parent:TweenPosition(UDim2.new(-1, 0, 0, 0))
					wait (1)
					script.Parent.Parent.Parent.Parent.Position = UDim2.new(1, 0, 0, 0)
					script.Parent.Parent.Parent.Question.Text = Config.questions[script.Value.Value]
					script.Parent.Parent.Parent.Parent:TweenPosition(UDim2.new(0, 0, 0, 0))
					local Ans = script.Parent.Text
						for i,v in pairs(Config.Keywords[script.Value.Value - 1])do
					if Ans:match(" "..v.." ") or Ans == v or Ans:sub(Ans:len()-(v:len() - 1),Ans:len()) == v or Ans:sub(1,v:len()) == v then
						game.ReplicatedStorage.Pass:FireServer()
					else
						print "User failed question"
				
					script.Parent.Text = ""
				
				wait (5)
			buttonPressed = false
		 
end
end
end
end 
end)


4 comments

0

Have you tried moving lines 28-29 to after the 'if buttonPressed == false then' statement?

0

would that not just disable to debounce immediately?

0

Add a wait after moving the lines.

0

There is a wait (5) at the bottom

1 answer

The problem is that you're adding your wait() and setting the debounce back to false at the wrong time. You have them in your else block, so they'll only run if the player does not meet the conditions on lines 9 and 21.

local Config1 = game.Workspace.Config
local Config = require(Config1)
local buttonPressed = false

script.Parent.Parent.Parent.Submit.MouseButton1Click:Connect(function()
	if buttonPressed == false then
		buttonPressed = true 
		script.Value.Value = script.Value.Value + 1
		if script.Value.Value == Config.questionAmount + 1 then
			local Value = script.Value.Value
			game.ReplicatedStorage.Check:FireServer(Value)
		else    

			script.Parent.Parent.Parent.Parent:TweenPosition(UDim2.new(-1, 0, 0, 0))
			wait (1)
			script.Parent.Parent.Parent.Parent.Position = UDim2.new(1, 0, 0, 0)
			script.Parent.Parent.Parent.Question.Text = Config.questions[script.Value.Value]
			script.Parent.Parent.Parent.Parent:TweenPosition(UDim2.new(0, 0, 0, 0))
			local Ans = script.Parent.Text
			for i,v in pairs(Config.Keywords[script.Value.Value - 1])do
				if Ans:match(" "..v.." ") or Ans == v or Ans:sub(Ans:len()-(v:len() - 1),Ans:len()) == v or Ans:sub(1,v:len()) == v then
                        		game.ReplicatedStorage.Pass:FireServer()
				else
                        		print "User failed question"

					script.Parent.Text = ""
				end
			end
		end
	else -- if buttonPressed is true because you forgot to add an else here
		print("Cooldown is not over yet!")
	end
	wait(5)
	buttonPressed = false -- there will always be a debounce, no matter which of the code executes
end)

1 comment

0

This didn't work, it still printed 5 times.

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