Local debounce not working

Hey!

I believe that this is a pretty simple issue, however I wasn’t able to fix it. I basically have a “menu” of 4 buttons, which then determine what’s gonna happen onwards. I obviously also want to make it so that when you click the already “active” button for the second time, it will make it “inactive” (change its Color3 etc.).

Everything worked, while I was using just one debounce variable, which was outside of all the functions (on a solo line). However, I realized that I need to implement the debounces locally. Because I also want player to disable the already active button, if they click other, not active one. (Click inactive Button2, makes already active Button1 inactive).

Though, it doesn’t work anymore after this change. Any idea what’s causing this issue?

local function trueDebounce()
	FrameCommunication.StationsMenu.Option1.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.Option2.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.Option3.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.Option4.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.ALL.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationOption.Value = ""
	FrameCommunication.MessageBox.TextBox.TextEditable = false
	FrameCommunication.MessageBox.TextBox.PlaceholderText = ""
	FrameCommunication.MessageBox.TextBox.Text = ""
	FrameCommunication.SendMessage.Visible = false
end

FrameCommunication.StationsMenu.Option1.MouseButton1Click:Connect(function()
	local localDebounce = false
	if localDebounce == false then
		print("localDebounce is FALSE")
		FrameCommunication.StationsMenu.Option1.BackgroundColor3 = Color3.fromRGB(152, 152, 152)
		FrameCommunication.StationsMenu.Option2.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.Option3.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.Option4.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.ALL.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationOption.Value = "Station1"
		FrameCommunication.MessageBox.TextBox.TextEditable = true
		FrameCommunication.MessageBox.TextBox.PlaceholderText = "Enter the message here."
		localDebounce = true
	elseif localDebounce == true then
		print("localDebounce is TRUE")
		trueDebounce()
		localDebounce = false
	end
end)
3 Likes

Oh btw, only the “localDebounce is FALSE” print keeps getting outprinted, the “TRUE” one never gets outprinted. So basically it’s not changing the debounce for some reason.

1 Like

This is a simple fix, you’re setting the debounce inside the MouseButton1Click event which will set it to false everytime the mouse is clicked, set the variable outside the event.

1 Like

I explained that in my post. I do not want it to work like that.

1 Like

Well, if the debounce is inside the MouseButton1Click event it will always be set to false when you click the button which means that the debounce will never be true.

3 Likes

Why does that happen though? Is it because the localDebounce variable is outside of the if check and the localDebounce = true event can’t access it?

What would you recommend to do for it to work?

2 Likes

This happens because the MouseButton1Click event fires everytime you click on the button and if the variable is set inside the event, everytime the button is clicked, the variable is set to false.

2 Likes

How can I fix this? Because I’d really like to make it locally, so that it doesn’t affect others buttons. (Explained furthermore in the post/original message)

2 Likes

Implement debounce for each button

1 Like

Well, tbh, I think the solution would be making different variables for each button.

2 Likes

Well I did that already… Every function has its own localDebounce variable. Or is it being accessed by the other buttons as well, because of the same name?

2 Likes

Make them have different names or the script won’t know which one to access.

2 Likes

It still doesn’t seem to work.

1 Like

Can you show me your script now?

2 Likes
local function trueDebounce()
	FrameCommunication.StationsMenu.Option1.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.Option2.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.Option3.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.Option4.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationsMenu.ALL.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
	FrameCommunication.StationOption.Value = ""
	FrameCommunication.MessageBox.TextBox.TextEditable = false
	FrameCommunication.MessageBox.TextBox.PlaceholderText = ""
	FrameCommunication.MessageBox.TextBox.Text = ""
	FrameCommunication.SendMessage.Visible = false
end

FrameCommunication.StationsMenu.Option1.MouseButton1Click:Connect(function()
	local debounce1 = false
	if debounce1 == false then
		print("localDebounce is FALSE")
		FrameCommunication.StationsMenu.Option1.BackgroundColor3 = Color3.fromRGB(152, 152, 152)
		FrameCommunication.StationsMenu.Option2.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.Option3.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.Option4.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.ALL.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationOption.Value = "Station1"
		FrameCommunication.MessageBox.TextBox.TextEditable = true
		FrameCommunication.MessageBox.TextBox.PlaceholderText = "Enter the message here."
		debounce1 = true
	elseif debounce1 == true then
		print("localDebounce is TRUE")
		trueDebounce()
		debounce1 = false
	end
end)
FrameCommunication.StationsMenu.Option2.MouseButton1Click:Connect(function()
	local debounce2 = false
	if debounce2 == false then
		FrameCommunication.StationsMenu.Option1.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.Option2.BackgroundColor3 = Color3.fromRGB(152, 152, 152)
		FrameCommunication.StationsMenu.Option3.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.Option4.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationsMenu.ALL.BackgroundColor3 = Color3.fromRGB(99, 99, 99)
		FrameCommunication.StationOption.Value = "Station2"
		FrameCommunication.MessageBox.TextBox.TextEditable = true
		FrameCommunication.MessageBox.TextBox.PlaceholderText = "Enter the message here."
	elseif debounce2 == true then
		trueDebounce()
		debounce2 = false
	end
end)
2 Likes

define above the functions they r still set to false all the time

2 Likes

Already said that, I don’t want it to work like that. It’s explained in the original message.

2 Likes

then dont use debounces ur code makes no sense

1 Like

It won’t work if the debounces are set inside of the MouseButton1Click event.

2 Likes

Lol I need to use debounces tho. That’s literally one of the key points of the system.

1 Like