Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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?

0
Sorry for all the questions lately, by the way. I'm just very terrible with UserInputService in general. OrcaTheFish 95 — 7y
0
bump OrcaTheFish 95 — 7y
0
BUMP OrcaTheFish 95 — 7y
0
Are you sure the animation isn't 5 secnds long? Bertox 159 — 7y
View all comments (2 more)
0
Oh, sorry, I forgot to mention that I did indeed fix it. OrcaTheFish 95 — 7y
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. OrcaTheFish 95 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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.

0
wait() times can vary depending on the length of your animation. Just keep that in mind. OrcaTheFish 95 — 7y
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() OrcaTheFish 95 — 7y
Ad

Answer this question