I'm just starting with lua coding, so the code can be very poorly written, but how to debounce this? I'm already tried several ways that turned out to be a failure so I decided to write about it here.
UIS.InputBegan:Connect(function(key,gameProcessed) if gameProcessed then return end if key.KeyCode == Enum.KeyCode.Z then char = plr.Character or plr.CharacterAdded:Wait() hum = char:WaitForChild('Humanoid') if char then anim = hum:LoadAnimation(animInst) end anim:Play() anim.KeyframeReached:connect(function(keyframename) if keyframename == 'Pause' then anim:AdjustSpeed(0) end end) end end) UIS.InputEnded:Connect(function(key,gameProcessed) if gameProcessed then return end if key.KeyCode == Enum.KeyCode.Z then anim:Stop() castMagic(plr, mouse.Hit.Position) end end)
so you would want to place the debounce right after the gameProccessed event like so.
local debounce = false UIS.InputBegan:Connect(function(key,gameProcessed) if gameProcessed then return end if debounce == false then debounce = true --run keycode statements wait(.5) -- or any time you want to wait fot the debounce debounce = false end end end)
Now if you click it no matter what it will wait(.5) until it can be activated again. However, I dont really know why you have that return end statement because it will then not even run the stop anim event.
UIS.InputEnded:Connect(function(key,gameProcessed) if gameProcessed then return end if key.KeyCode == Enum.KeyCode.Z then anim:Stop() castMagic(plr, mouse.Hit.Position) end end)
In order to implement debounce, you need to have a variable that is set to something. You then set the function to require the debounce to have that originally set "something". In the function, you'll change the debounce to something else.
So in the example below, I have a debounce variable set to false at the beginning.
local debounce = false if debounce == false then debounce = true print("Function Executed") -- Output: "Function Executed" wait(3) debounce == false end
If this helped, consider accepting this answer. If not, comment below and I'll be sure to help you out further.