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

How do I prevent an animation from interrupting?

Asked by
MrHerkes 166
7 years ago

I made a animation that punches and hurt people. The animation interrupts when I press the mouse button many times. I want it to only play again if the animation is done.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:connect(function()
    local animation = Instance.new("Animation")
    animation.AnimationId="http://www.roblox.com/asset/?id=791758753"
    local character = Player.Character
    local anim = character.Humanoid:LoadAnimation(animation)
    anim:Play()
    wait(3)
    character.RightHand.Touched:connect(function(hand)
        local humanoid = hand.Parent:FindFirstChild("Humanoid")
        if (humanoid ~= nil) then
            humanoid.Health = 0
            hand.Velocity = (hand.CFrame.lookVector) * -50
        end
    end)    
end)

Bonus Question: How do I hurt a humanoid when the animation is playing and touching the humanoid? When the humanoid touches my hand, it hurts them when i'm not playing the animation. Hope this makes sense.

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You can simply check the IsPlaying state of the AnimationTrack prior to loading and playing it. You're gonna have to make a variable to hold the AnimationTrack.

Also, to make the Touched event fire you should define it outside of the Button1Down event.

You may want to add a debounce.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local track --Make a holder for the AnimationTrack

repeat wait() until Player.Character

local character = Player.Character

character:WaitForChild("RightHand").Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
hit.Velocity = (hand.CFrame.lookVector) * -50
    end
end)    

Mouse.Button1Down:connect(function()
    if not track and not track.IsPlaying then
        local animation = Instance.new("Animation")
        animation.AnimationId="http://www.roblox.com/asset/?id=791758753"
        local anim = character.Humanoid:LoadAnimation(animation)
        track = anim;
        anim:Play()
        wait(3)
    end
end)
0
Explain line 3 please. MrHerkes 166 — 7y
0
I believe I already did with the comment Goulstem 8144 — 7y
0
*facepalm* MrHerkes 166 — 7y
Ad

Answer this question