In game, if I punch, I can't do so again for roughly 5 seconds. Why is this? It's quite annoying, I really would like to get rid of it.
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:wait() local Hum = char:WaitForChild("Humanoid") local animation = Instance.new("Animation") animation.AnimationId = "rbxassetid://855470374" -- Punching Animation wait() local animTrack = Hum:LoadAnimation(animation) game:GetService("UserInputService").InputBegan:connect(function(Key) if Key.KeyCode == Enum.KeyCode.E then animTrack:Play() -- Initiate the Punching Animation end end)
Is it a problem with my punching animation, or the script?
I guess I'll answer my own question now that I fixed it, just for the people who want to know. I set a variable, (punchinprogress). This variable would act as the restraining order for the pressing of the key E.
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:wait() local Hum = char:WaitForChild("Humanoid") local animation = Instance.new("Animation") local punchinprogress = 0 animation.AnimationId = "rbxassetid://855470374" -- Punching Animation wait() local animTrack = Hum:LoadAnimation(animation) game:GetService("UserInputService").InputBegan:connect(function(Key) if Key.KeyCode == Enum.KeyCode.E and punchinprogress == 0 then punchinprogress = 1 print("Punch Triggered") animTrack:Play() -- Initiate the Punching Animation wait(0.8) animTrack:Stop() punchinprogress = 0 print("Punch Accomplished") end end)
As you can see, E can only trigger the events followed if punchinprogress == 0. If punchinprogress == 1, then the function would be unable to continue on with its work. If punchinprogress = 0, then it would immediately set punchinprogress to 1 to prevent further spamming. It then initiates the animation, waits for the animation to end, stops the animation, and sets punchinprogress to 0 so the process can be repeated.