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

Idle animation wont stop playing?

Asked by 6 years ago

So ,i have a script for my sword

local CanAttack = true

script.Parent.Equipped:connect(function()
    local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
    local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)

    idle:Play()
end)

script.Parent.Activated:connect(function()
    local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
    local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)

    if CanAttack == true then
        attack:Play()
        idle:Stop()
        CanAttack = false
        wait(1)
        attack:Stop()
        idle:Play()
        CanAttack = true
        script.Parent.canDamage.Value = true
    end
end)

script.Parent.Unequipped:Connect(function()
    local character = game.Players.LocalPlayer.Character
    local idle = character.Humanoid:LoadAnimation(script.Idle)  

    idle:Stop()
end)

basically it plays the idle animation unless the tool is activated, then it will run the attack animation and go back to the idle animation after 1 second.

HOWEVER, the problem is.. when i unequipped the sword my character model still plays the idle animation, so he pretty much runs around with his hand up but no sword in it.

2 answers

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago

idle needs to be global across the script. Don't use LoadAnimation every time you want to play/stop it, because that would make a new animation playing on top of the other one. I might also suggest removing the code on line 6 as you don't use it in that function.

local CanAttack = true
local idle = nil

script.Parent.Equipped:connect(function()
    idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
    local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)

    idle:Play()
end)

script.Parent.Activated:connect(function()
    local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)

    if CanAttack == true then
        attack:Play()
        idle:Stop()
        CanAttack = false
        wait(1)
        attack:Stop()
        idle:Play()
        CanAttack = true
        script.Parent.canDamage.Value = true
    end
end)

script.Parent.Unequipped:Connect(function()
    local character = game.Players.LocalPlayer.Character

    idle:Stop()
    idle:Destroy()
end)
0
awesome! this code worked! i do have a question though, what did making local idle = nil do? and also, what is the difference of having local in front of a variable name when creating it do? PoePoeCannon 519 — 6y
0
hey can i ask why you made a local variable character at the bottom? PoePoeCannon 519 — 6y
0
Actually, it doesn't need to be there. But if you want to access the character, put the variable at the top so you can use it across the whole script. UgOsMiLy 1074 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You need to put, on unequip stop animation. Because the animation plays on equip, so the script does not have a stop point it still thinks the sword is equipped. So it is still playing the animation. When you put it in a stop point the script will know the sword is not equipped so I need to stop playing animation.

Answer this question