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

Can't punch for a few seconds after a punch?

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?

6 comments

0

Sorry for all the questions lately, by the way. I'm just very terrible with UserInputService in general.

0

bump

0

BUMP

0

Are you sure the animation isn't 5 secnds long?

0

Oh, sorry, I forgot to mention that I did indeed fix it.

0

I created a variable to block the user from triggering an event when E is pressed if it equaled one. Then, I made it to where if you pressed E, it would set it to 1, wait 0.9 seconds, stop the animation, and set the variable to 0.

1 answer

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.

2 comments

0

wait() times can vary depending on the length of your animation. Just keep that in mind.

0

Also, forgot to mention, it was probably a problem with the animation length. Thats why I would probably do a wait timer and then just do animTrack:Stop()

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