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

Can someone help me with my debounce feature?

I understand debounce, but I want to know if I'm using it right. I'm making a toggleable FOV slider for my flight simulator game, and I want to have a little button toggling it from the side. Currently, only closing it works. Help?

local debounce = false
local button = script.Parent
local frame = script.Parent.Parent

script.Parent.MouseButton1Up:Connect(function()
	if debounce == false then
		--tween the gui in
		debounce = true
		frame:TweenPosition(
			UDim2.new(1.062,-60,1,-210),
			"In",
			"Sine",
			0.4,
			false
		)
		button.Text = "<"
	end
	
	if debounce == true then
		debounce = false
		frame:TweenPosition(
			UDim2.new(1,-60,1,-210),
			"In",
			"Sine",
			0.4,
			false
		)
		button.Text = ">"
	end
end)

Any help would be appreciated.

2 answers

✓ Accepted answer

You're changing debounce before it reaches the second if statement, so the second if statement will proceed. Try using a elseif as shown in this script:

local debounce = false
local button = script.Parent
local frame = script.Parent.Parent

script.Parent.MouseButton1Up:Connect(function()
	if not debounce then
		--tween the gui in
		debounce = true
		frame:TweenPosition(
			UDim2.new(1.062,-60,1,-210),
			"In",
			"Sine",
			0.4,
			false
		)
		button.Text = "<"
		
	elseif debounce then -- if debounce is true, then this statement will run. If debounce isnt true, then the first if statement will run
		debounce = false
		frame:TweenPosition(
			UDim2.new(1,-60,1,-210),
			"In",
			"Sine",
			0.4,
			false
		)
		button.Text = ">"
	end
end)

2 comments

0

Hi! Thanks for answering, although it is clear that this answer could be improved. Since I don't want to make another answer and possibly "steal" your reputation, could you please change the following things?

1

"if not debounce then" instead of "if debounce == false" and "elseif debounce then" instead of "elseif debounce == true"

The way you used debounce is a sort of Enabled/Disabled value. Debounce is made to add a cooldown so that people won't spam whatever function you've created.

local debounce = false
local button = script.Parent
local frame = script.Parent.Parent

script.Parent.MouseButton1Up:Connect(function()
	if debounce == false then
		
		debounce = true
		frame:TweenPosition(
			UDim2.new(1.062,-60,1,-210),
			Enum.EasingDirection.In,
			Enum.EasingStyle.Sine,
			0.4,
			false
		)
		button.Text = "<"
		
		wait(1) --debounce is to prevent spamming, so u must add a wait
		debounce = false --then disable debounce again so the function can be used again
	end
end)

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