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

Why doesn't my idle animation play while I stand still?

Asked by 3 years ago

I made a script for an axe that swings (animation only no damage) and has an Idle stance when standing still. Everything works except for the idle animation. I don't know what's wrong. I'm a bit of a beginner. This is all in a local script

wait(5)
tool = script.Parent
local player = game.Players.LocalPlayer
local hum = player.Character.Humanoid
local anim1 = Instance.new("Animation")
anim1.AnimationId = "rbxassetid://7253655066"
local stance = hum:LoadAnimation(anim1)
local ten = true
local swinging = false
local en = true
local on = 1

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://7260872928"
local axe1 = hum:LoadAnimation(anim2)

local anim3 = Instance.new("Animation")
anim3.AnimationId = "rbxassetid://7264662836"
local axe2 = hum:LoadAnimation(anim3)

local anim4 = Instance.new("Animation")
anim4.AnimationId = "rbxassetid://7264665302"
local axe3 = hum:LoadAnimation(anim4)

while tool.Equipped == true and hum.MoveDirection.Magnitude == 0 do
    stance:Play()
    stance.Looped = true
    if hum.MoveDirection.Magnitude > 0 then
        stance:Stop()
    end
end


tool.Activated:Connect(function()
    if en == false then return end
    en = false
    local char = tool.Parent
    local hum = char.Humanoid
    local anim = hum:LoadAnimation (tool.Handle ["Animation"..on]) -- Animation 1 or Animation 2
    anim:Play()
    swinging = true
    ten = true
    wait(0.4)
    swinging = false
    on = on + 1
    if on >= 4 then
        on = 1
        wait(1)
    end
    en = true
end)

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I recommend changing your method of loading animations, as Humanoid:LoadAnimation() has been deprecated. Try using Animator:LoadAnimation() instead.

Edit: My bad, didn't read the full script. Since you have tool.Equipped == true and hum.MoveDirection.Magnitude == 0 in the conditional segment for your while loop, if those conditions aren't met, the while loop breaks. So we'll move it into an if statement:

while true do
    if tool.Equipped == true and hum.MoveDirection.Magnitude == 0 then
        stance:Play()
        stance.Looped = true
        if hum.MoveDirection.Magnitude > 0 then
            stance:Stop()
        end
    end
    wait()
end

Now that we've done that, we have run into another issue. If you were to test with this fixed while loop, you'll notice that the animation will keep restarting every 0.03 seconds. To fix that, we'll check if the animation is playing using a property called IsPlaying. Add in another if statement and check if the animation is already playing:

while true do
    if tool.Equipped and hum.MoveDirection.Magnitude == 0 then
        if not stance.IsPlaying then
            stance:Play()
            stance.Looped = true
        end

        if hum.MoveDirection.Magnitude > 0 then
            stance:Stop()
        end
    end
    wait()
end
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

while wait() do if tool.Equipped == true and hum.MoveDirection.Magnitude == 0 then stance:Play() stance.Looped = true elseif hum.MoveDirection.Magnitude > 0 then stance:Stop() end end

0
wtf is this? JesseSong 3916 — 3y

Answer this question