Debounce not working correctly

Hey! I’m trying to loop through all Frames that are a child of a ScrollingFrame and then trigger a function once a TextButton inside those Frames is clicked.

I’d like to make it so that when you click from one Frame (TextButton) to another (when you can, aka when the lock is invisible), it obviously changes the InfoFrame properties, but also that if you click the same Frame (TextButton) twice in a row, it will make the InfoFrame invisible again. (debounce which either makes it visible or invisible).

Though, it only works once. It doesn’t set the debounce to false for some reason.

local Players = game:GetService("Players")
local plr = Players.LocalPlayer

local ConstructionFolder = plr:WaitForChild("ConstructionFolder")

local GUI = script.Parent.Holder
local TaskFrame = GUI.Tasks
local InfoFrame = GUI.Info

local buttonDebounce = false

local function changeVisibility(value)
	InfoFrame.StartTask.Visible = value
	InfoFrame.Description.Visible = value
	InfoFrame.Header.Visible = value
	InfoFrame.ProgressCount.Visible = value
	InfoFrame.TaskProgress.Visible = value
	buttonDebounce = value
end

for i, v in pairs(TaskFrame.ScrollingFrame:GetChildren()) do
	if v.ClassName == "Frame" then
		if v.Lock.Visible == false then
			if buttonDebounce == false then
				v.SelectButton.MouseButton1Click:Connect(function()
					changeVisibility(true)
					InfoFrame.Header.Text = v.TaskName.Text
					InfoFrame.Description.Text = v.Description.Value
					local findFolder = ConstructionFolder:FindFirstChild(v.TaskName.Text.."Folder")
					local findX = findFolder:FindFirstChild(v.TaskName.Text.."X")
					local findY = findFolder:FindFirstChild(v.TaskName.Text.."Y")
					InfoFrame.ProgressCount.Text = findX.Value.."/"..findY.Value
					print(buttonDebounce)
				end)
			elseif buttonDebounce == true then
				v.SelectButton.MouseButton1Click:Connect(function()
					changeVisibility(false)
					print(buttonDebounce)
				end)
			end
		end
	end
end

Hello, what does the console say about it?

It keeps printing true, never prints false.

oh well, I think you should put the condition if buttonDebounce == false then & the other one in the MouseButton1Click

1 Like

Try that instead

local Players = game:GetService("Players")
local plr = Players.LocalPlayer

local ConstructionFolder = plr:WaitForChild("ConstructionFolder")

local GUI = script.Parent.Holder
local TaskFrame = GUI.Tasks
local InfoFrame = GUI.Info

local buttonDebounce = false

local function changeVisibility(value)
	InfoFrame.StartTask.Visible = value
	InfoFrame.Description.Visible = value
	InfoFrame.Header.Visible = value
	InfoFrame.ProgressCount.Visible = value
	InfoFrame.TaskProgress.Visible = value
	buttonDebounce = value
end

for i, v in pairs(TaskFrame.ScrollingFrame:GetChildren()) do
	if v.ClassName == "Frame" then
		if v.Lock.Visible == false then
				v.SelectButton.MouseButton1Click:Connect(function()
        if buttonDebounce == false then
					changeVisibility(true)
					InfoFrame.Header.Text = v.TaskName.Text
					InfoFrame.Description.Text = v.Description.Value
					local findFolder = ConstructionFolder:FindFirstChild(v.TaskName.Text.."Folder")
					local findX = findFolder:FindFirstChild(v.TaskName.Text.."X")
					local findY = findFolder:FindFirstChild(v.TaskName.Text.."Y")
					InfoFrame.ProgressCount.Text = findX.Value.."/"..findY.Value
					print(buttonDebounce)
			elseif buttonDebounce == true then
					changeVisibility(false)
					print(buttonDebounce)
			end
		end)
		end
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.