I'm a beginner to Roblox scripting and I was trying to make a combat script and I wanted to add a delay after you press a key to attack to prevent just outright button spamming combat but it seems like wait(n) is not working? I've tried various different values and it just does not work for some reason. Any help would be appreciated, thanks in advance.
-- Delays
local punchDelay = 1.5 -- !! MOVE TO SERVERSIDE !!
local kiBlastDelay = 2.5 -- !! MOVE TO SERVERSIDE !!
--- Actions
local function punch()
print("punch()")
-- !! MAKE THIS SERVERSIDE !! (Remote functions)
-- TO-DO: animation
-- TO-DO: damage n stuff
-- TO-DO: increase stats
game.ReplicatedStorage.playSound:FireServer(0)
game.ReplicatedStorage.IncreaseXp:FireServer()
end
local function kiBlast()
print("kiBlast()")
-- !! MAKE THIS SERVERSIDE !! (Remote functions)
-- TO-DO: animation
-- TO-DO: damage n stuff
-- TO-DO: increase stats
game.ReplicatedStorage.playSound:FireServer(1)
game.ReplicatedStorage.IncreaseXp:FireServer()
end
userInputService.InputBegan:Connect(function(input, GPE)
local lastInputEnum = userInputService:GetLastInputType()
if lastInputEnum == Enum.UserInputType.Keyboard or string.find(tostring(lastInputEnum.Name), "MouseButton") or lastInputEnum == Enum.UserInputType.MouseWheel then
-- Keyboard
if input.KeyCode == keyboard_punch then punch() wait(punchDelay)
elseif input.KeyCode == keyboard_kiBlast then kiBlast() wait(kiBlastDelay) end
elseif string.find(tostring(lastInputEnum.Name), "Gamepad") then
-- Gamepad (Controller)
if input.KeyCode == gamepad_punch then punch() wait(punchDelay)
elseif input.KeyCode == gamepad_kiBlast then kiBlast() wait(kiBlastDelay) end
end
wait(.1)
end)

2 comments
You may also want to add the wait in a spawn function if you do not want to hold up the rest of the code
It worked, thank you! I tried to implement a debounce earlier but I failed and thought it was only for physic-related stuff, btw I wasn't using the .1 as delay but the delay variables (punchDelay + kiBlastDelay)