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

Debounce is not Working on the UserInputService?

Idea is, a cooldown so they don't spam the Z Key, they have to wait 6 seconds. But I can still spam the key, even though i put the debounce there, am I doing something wrong? I don't get any warnings or errors.

UIS.InputBegan:Connect(function(key, isTalking) 
	if isTalking then return end
	
	--------------------------FIRST POWER----------------------------------------------
	local Debounce = false
	
	if key.KeyCode == Enum.KeyCode.Z then
		if Debounce == false then
			Debounce = true
			
			print("Clicked Z!")
			game.ReplicatedStorage.EEnergy8Spells.firstSpell.firstMagic:FireServer(Mouse.Hit)
			wait(6)
			Debounce = false
		end
	end
end)

3 answers

✓ Accepted answer

Every time someone presses Z in your case, it creates new debounce variable in the function's enviroment so only the function can see it, all you have to do is put the debounce variable before the function:

--/ Put the variable in script's enviroment so not only the function
--\ can see but the whole script, this will let you to reuse the variable
--\ more times in the script
local Debounce = false

UIS.InputBegan:Connect(function(key, isTalking) 
    if isTalking then return end

    --------------------------FIRST POWER----------------------------------------------
    if key.KeyCode == Enum.KeyCode.Z then
        if Debounce == false then
            Debounce = true

            print("Clicked Z!")
            game.ReplicatedStorage.EEnergy8Spells.firstSpell.firstMagic:FireServer(Mouse.Hit)
            wait(6)
            Debounce = false
        end
    end
end)

Now this will change debounce variable that the whole script can see so every time new function fires it takes previous debounce variable and checks if that one is false/true.

Also make sure to create similar for each player on the server as exploiter can just remove the debounce and... he will fire spells faster that you need

I would either say 3 options to try and fix this problem.

1: make DEBOUNCE into enabled or something different because debounce is acting like a key word and it is a cooldown basically. This means that probably debounce wont work that's highly unlikely though.

2: Make the code so it has the following:

	UIS.InputBegan:Connect(function(key, isTalking)
	    if isTalking then return end
	 
	    --------------------------FIRST POWER----------------------------------------------
	    local Debounce = false
	 
	    if key.KeyCode == Enum.KeyCode.Z and Debounce == false then
	            Debounce = true
	            print("Clicked Z!")
	            game.ReplicatedStorage.EEnergy8Spells.firstSpell.firstMagic:FireServer(Mouse.Hit)
	            wait(6)
	            Debounce = false
	        end
	    end
	end)

This will make it so if the input is z and the debounce is false in the same line then it will run. You can also make key turn into INPUT and you would do:

if input.KeyCode == Enum.KeyCode.Z and Debounce == false then

end

3: You can change the wording of it. Such as the following:

local Debounce = false
if key.KeyCode == Enum.KeyCode.Z then
if not Debounce then

Debounce = true
--Code Here


Debounce = false
	end
end

If none of these work try a pcall and look for an error through that as they catch almost all the errors OR add waits into your problem.

Hope this helped!

1 comment

0

I tried both of them, and none of them unfortunately worked, thanks for trying!

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