Debounce not working

Hello, there. I’m trying to make this button that’s repeatable but it doesn’t seem to be working. There’s no error popping up in error. I’m just trying to make this to work, i even made a post, no one responded to that. probably no one will respond to this one also

-- ButtonActions
-- Button Tween
local debounce = 0

local TweenService = game:GetService("TweenService")
local ButtonStages = {
	DefaultColor = Color3.fromRGB(138, 255, 146),
	PressedColor = Color3.fromRGB(0, 0, 0)
}

local ButtonTweenInfo = TweenInfo.new(
	.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

-- Variables
local ButtonActions = {}
local ButtonsFolder = workspace:WaitForChild("Game"):WaitForChild("Buttons")

-- Function
local function GetButtonByID(id)
	for _, child in ipairs(ButtonsFolder:GetChildren()) do
		child:WaitForChild("ID")
		if child.ID.Value == id then
			return child
		end
	end
end

function ButtonActions:Run(id)
	local button = GetButtonByID(id)
	if button then
		button:WaitForChild("Button")
		local ButtonTween = TweenService:Create(button.Button, ButtonTweenInfo, {Position = button.Button.Position + Vector3.new(0, -.3, 0), Color = ButtonStages.PressedColor})
		ButtonTween:Play()
		require(script:WaitForChild(tostring(id))):Execute()
		wait(2)
		debounce = 1
	end
end

-- Return
return ButtonActions
-- ThIs will tell what the obstacle will do

-- Services
local debounce = 0

local TweenService = game:GetService("TweenService")

 -- Variables
local Obstacle = game.Workspace.Game.Obstacles.ObstaclePartA

local ObstacleTweenInfo = TweenInfo.new(
	0,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local Functions = {}

-- Execute
function Functions:Execute()
	print("Button with the ID" .. script.Parent.Name .."was pressed")
	local ObstacleTween = TweenService:Create(Obstacle, ObstacleTweenInfo, {Transparency = 1, Position = Vector3.new(0, -25, 0)})
	ObstacleTween:Play()
	wait(5)
	debounce = 1
end

-- Return
return Functions

Are you requiring this ModuleScript?

Yes, i’m using it for a game. And, when it’s done, i’m planning it to make open-sourced.

I only see that you are setting denounce to 1, but you are not checking the debounce.

How will i do that? I’m sorry, i’m only starting to learn lua

You would do this:

if debounce == 0 then -- check
    debounce = 1 -- set to 1
    -- insert code
    debounce = 0 -- reset to 0
end

Yep, it stil doesn’t work… It just seems impossible to do it

Are you requiring the first script too?

there’s a few topics you might want to consider before creating another one.

And here:

if debounce == 0 then – check
debounce = 1 – set to 1
– insert code
debounce = 0 – reset to 0
end

remember to yield somewhere in there, just mentioning that to clarify the outline.

1 Like

I’m sorry i don’t which keywords i will use because i don’t understand lua quite yet

Yes, the player won’t be able to press it again without it

It still doesn’t work. I tried to put in both of the scripts, it still doesn’t work

This script will tell what the obstacle will do:

if debounce == 0 then
	debounce = 1
end

local TweenService = game:GetService("TweenService")

 -- Variables
local Obstacle = game.Workspace.Game.Obstacles.ObstaclePartA

local ObstacleTweenInfo = TweenInfo.new(
	30,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local Functions = {}

-- Execute
function Functions:Execute()
	print("Button with the ID" .. script.Parent.Name .."was pressed")
	local ObstacleTween = TweenService:Create(Obstacle, ObstacleTweenInfo, {Transparency = 0, Position = Vector3.new(-86.45, -4.5, 75)})
	ObstacleTween:Play()
	wait(2)
	debounce = 0
end

-- Return
return Functions

Button Actions:

-- Button Tween
local TweenService = game:GetService("TweenService")
local ButtonStages = {
	DefaultColor = Color3.fromRGB(138, 255, 146),
	PressedColor = Color3.fromRGB(0, 0, 0)
}

local ButtonTweenInfo = TweenInfo.new(
	.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

-- Variables
local ButtonActions = {}
local ButtonsFolder = workspace:WaitForChild("Game"):WaitForChild("Buttons")

-- Function
local function GetButtonByID(id)
	for _, child in ipairs(ButtonsFolder:GetChildren()) do
		child:WaitForChild("ID")
		if child.ID.Value == id then
			return child
		end
	end
end

function ButtonActions:Run(id)
	local button = GetButtonByID(id)
	if button then
		button:WaitForChild("Button")
		local ButtonTween = TweenService:Create(button.Button, ButtonTweenInfo, {Position = button.Button.Position + Vector3.new(0, -.3, 0), Color = ButtonStages.PressedColor})
		ButtonTween:Play()
		require(script:WaitForChild(tostring(id))):Execute()
	end
end

-- Return
return ButtonActions

Button Handler: (i forgot to add this earlier)

local ButtonActions = require(script.Parent.ButtonActions)

local Buttons = {}
local ButtonHandler = {}

local buttonsFolder: Folder = workspace:WaitForChild("Game"):WaitForChild("Buttons")

local function FindButton(id)
	for _, child in ipairs(buttonsFolder:GetChildren()) do
		if child.Name == "ButtonModel" then
			child:WaitForChild("ID")
			if child.ID.Value == id then
				return child
			end
		end
	end	
end

function ButtonHandler:Init()
	for _, child in ipairs(buttonsFolder:GetChildren()) do
		if child.Name == "ButtonModel" then
			child:WaitForChild("ID")
			Buttons[child.ID.Value] = false
		end
	end
end

local OnChildAdded = function (child)
	child:WaitForChild("Button")
	child:WaitForChild("ID")
	child.Button.Touched:Connect(function ()
		if Buttons[child.ID.Value] == true then return end
		Buttons[child.ID.Value] = true
		ButtonActions:Run(child.ID.Value)
	end)
end

buttonsFolder.ChildAdded:Connect(OnChildAdded)
for _, child in ipairs(buttonsFolder:GetChildren()) do
	OnChildAdded(child)
end

return ButtonHandler