Weird issue with Debounce

Heys, so i’m having a very weird issue.

So here, I made a simple UserInputService system which fires a remote event when a key is pressed.

The issue is, when the Duration of the spell is done, you can do the spell again.
What’s supposed to happen is, when the duration is done, it waits the cooldown aswell. And once the cooldown is done you can do it.

I tried printing debounce and it prints false everytime, even tough I only changed it back to false after the cooldown and not after the duration.

uis.InputBegan:Connect(function(input, gp)
	if gp then return end
	if not toolEquipped then return end
	if mouse.Target == nil then return end
	
	local function DetectInput(skillConfig, debounce)
		if input.KeyCode == Enum.KeyCode[tostring(skillConfig:WaitForChild("Key").Value)] then
			if toolEquipped then
				if not debounce and canAttack.Value == true then
					if level.Value >= skillConfig:WaitForChild("LevelRequired").Value then
						
						print(debounce)
						debounce = true
						remote:FireServer(mouse.Hit.p, skillConfig.Name)
						changeCanAttack:FireServer(false)
						
						wait(skillConfig:WaitForChild("Duration").Value)
						
						toolUI:WaitForChild(skillConfig.Name):WaitForChild("Cooldown").BackgroundTransparency = 0
						ts:Create(toolUI:WaitForChild(skillConfig.Name):WaitForChild("Cooldown"), TweenInfo.new(skillConfig:WaitForChild("Cooldown").Value, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
						changeCanAttack:FireServer(true)
						
						debounce = true
						
						wait(skillConfig:WaitForChild("Cooldown").Value)
						
						debounce = false
					else
						warn("Couldn't do the attack. Please reach the required level ("..skillConfig:WaitForChild("LevelRequired").Value..") to perfom it.")
					end
				end
			end
		end
	end
	
	DetectInput(skillConfigs:WaitForChild("Fireball"), debounce1)
	DetectInput(skillConfigs:WaitForChild("Fire Breath"), debounce2)
	DetectInput(skillConfigs:WaitForChild("Blazing Barrier"), debounce3)
	DetectInput(skillConfigs:WaitForChild("Fire Projectiles"), debounce4)
	DetectInput(skillConfigs:WaitForChild("Nova Bomb"), debounce5)
end)

Any comment helps. :slight_smile:

  • Zakres
1 Like

What is debounce1, debounce2, debounce3 and so on on the last part of the script?

1 Like

I believe the reason it is printing false is because you print it before you make it true.


uis.InputBegan:Connect(function(input, gp)
        if gp then return end
        if not toolEquipped then return end
        if mouse.Target == nil then return end

        local function DetectInput(skillConfig, debounce)
            if input.KeyCode == Enum.KeyCode[tostring(skillConfig:WaitForChild("Key").Value)] then
                if toolEquipped then
                    if not debounce and canAttack.Value == true then
                        if level.Value >= skillConfig:WaitForChild("LevelRequired").Value then
                            debounce = true
                            print(debounce)
                            remote:FireServer(mouse.Hit.p, skillConfig.Name)
                            changeCanAttack:FireServer(false)

                            wait(skillConfig:WaitForChild("Duration").Value)

                            toolUI:WaitForChild(skillConfig.Name):WaitForChild("Cooldown").BackgroundTransparency = 0
                            ts:Create(toolUI:WaitForChild(skillConfig.Name):WaitForChild("Cooldown"), TweenInfo.new(skillConfig:WaitForChild("Cooldown").Value, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
                            changeCanAttack:FireServer(true)

                            wait(skillConfig:WaitForChild("Cooldown").Value)

                            debounce = false
                        else
                            warn("Couldn't do the attack. Please reach the required level ("..skillConfig:WaitForChild("LevelRequired").Value..") to perfom it.")
                        end
                    end
                end
            end
        end

        DetectInput(skillConfigs:WaitForChild("Fireball"), debounce1)
        DetectInput(skillConfigs:WaitForChild("Fire Breath"), debounce2)
        DetectInput(skillConfigs:WaitForChild("Blazing Barrier"), debounce3)
        DetectInput(skillConfigs:WaitForChild("Fire Projectiles"), debounce4)
        DetectInput(skillConfigs:WaitForChild("Nova Bomb"), debounce5)
    end)

1 Like

Just a bunch of variables set to false.
image

Yeah, but the problem is, it’s not supposed to print false since I only made the debounce back to true after waiting the Duration and Cooldown.

But here, what’s happening is, when I do the attack, I can already do it again after the duration. It seems like it’s ignoring the cooldown somehow.

i think i know what is causing the issue, you are basically re-creating the function every time you activate it, your function should be created before the main script.

local function DetectInput(skillConfig, debounce)
		if input.KeyCode == Enum.KeyCode[tostring(skillConfig:WaitForChild("Key").Value)] then
			if toolEquipped then
				if not debounce and canAttack.Value == true then
					if level.Value >= skillConfig:WaitForChild("LevelRequired").Value then
						
						print(debounce)
						debounce = true
						remote:FireServer(mouse.Hit.p, skillConfig.Name)
						changeCanAttack:FireServer(false)
						
						wait(skillConfig:WaitForChild("Duration").Value)
						
						toolUI:WaitForChild(skillConfig.Name):WaitForChild("Cooldown").BackgroundTransparency = 0
						ts:Create(toolUI:WaitForChild(skillConfig.Name):WaitForChild("Cooldown"), TweenInfo.new(skillConfig:WaitForChild("Cooldown").Value, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
						changeCanAttack:FireServer(true)
						
						debounce = true
						
						wait(skillConfig:WaitForChild("Cooldown").Value)
						
						debounce = false
					else
						warn("Couldn't do the attack. Please reach the required level ("..skillConfig:WaitForChild("LevelRequired").Value..") to perfom it.")
					end
				end
			end
		end
	end

uis.InputBegan:Connect(function(input, gp)
	if gp then return end
	if not toolEquipped then return end
	if mouse.Target == nil then return end
	DetectInput(skillConfigs:WaitForChild("Fireball"), false)
	DetectInput(skillConfigs:WaitForChild("Fire Breath"), false)
	DetectInput(skillConfigs:WaitForChild("Blazing Barrier"), false)
	DetectInput(skillConfigs:WaitForChild("Fire Projectiles"), false)
	DetectInput(skillConfigs:WaitForChild("Nova Bomb"), false)
end)

Something like this.

1 Like

never mind … I see that isn’t it …

1 Like

I’ll try that out, thanks a lot!

1 Like

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