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

How can I get rid of a delay when an animation plays?

Asked by 3 years ago

I'm trying to make an animation script for being idle and walking. I know that I can change the default animation, but I'm looking to do something else with this. My problem is that there's a delay between the player moving and the animation playing. I'm also using a script inside ServerScriptService instead of a local script because I don't think a local script would animate on both client and server(if it does please let me know). If it's because of an issue with the animation itself, please let me know and I'll fix that.

game.Players.PlayerAdded:Connect(function(plr)
    repeat wait() until plr.Character
    local char = plr.Character
    local Humanoid = char.Humanoid


    local idle = Instance.new("Animation")
    idle.AnimationId = "rbxassetid://7052225614"
    local idletrack = Humanoid:LoadAnimation(idle)
    local move = Instance.new("Animation")
    move.AnimationId = "rbxassetid://7052226982"
    local movetrack = Humanoid:LoadAnimation(move)

    local function playAnimation(animName) 
        if animName == "move" then
            movetrack:Play()
        end
        if animName == "idle" then
            idletrack:Play()
        end
    end

    local function stopAnimation(animName) 
        if animName == "move" then
            movetrack:Stop()
        end
        if animName == "idle" then
            idletrack:Stop()
        end
    end

    playAnimation("idle") --To load it when the player spawns

    Humanoid.Running:Connect(function(speed)
        if speed > 0 then
            if not movetrack.IsPlaying then
                stopAnimation("idle")
                playAnimation("move")
            end
        else
            if speed == 0 then
                if not idletrack.IsPlaying then
                    stopAnimation("move")
                    playAnimation("idle")
                end
            end
        end
    end)
end)
1
Maybe try using elseif? You're not using elseif anywhere and maybe that's causing the delay. ZIRFAL3 17 — 3y

2 answers

Log in to vote
1
Answered by
ZIRFAL3 17
3 years ago
Edited 3 years ago
game.Players.PlayerAdded:Connect(function(plr)
    repeat wait() until plr.Character
    local char = plr.Character
    local Humanoid = char.Humanoid

    local idle = Instance.new("Animation")
    idle.AnimationId = "rbxassetid://7052225614"
    local idletrack = Humanoid:LoadAnimation(idle)
    local move = Instance.new("Animation")
    move.AnimationId = "rbxassetid://7052226982"
    local movetrack = Humanoid:LoadAnimation(move)

    local function playAnimation(animName)
            if animName == "move" then
                movetrack:Play()
            elseif animName == "idle" then
                idletrack:Play()
            end
        end

    local function stopAnimation(animName)
        if animName == "move" then
            movetrack:Stop()
        elseif animName == "idle" then
            idletrack:Stop()
        end
    end

        playAnimation("idle") --To load it when the player spawns

    Humanoid.Running:Connect(function(speed)
        if speed > 0 then
            if not movetrack.IsPlaying then
                stopAnimation("idle")
                playAnimation("move")

            end
        elseif speed == 0 then
            if not idletrack.IsPlaying then

                stopAnimation("move")
                playAnimation("idle")

            end
        end
    end)
end)

There.

0
I think I messed up the script.. ZIRFAL3 17 — 3y
0
Too many elseifs. OwOShiba 78 — 3y
0
Did you try it? ZIRFAL3 17 — 3y
0
Let me edit this so It is more ordered ZIRFAL3 17 — 3y
0
tidy* ZIRFAL3 17 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Fixed thanks to the comments and almost answer. The delay is mostly gone, but a slight delay is fine. I needed to add elseifs to the functions but not the Humanoid.Running.

Answer this question