Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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 comment

1

Maybe try using elseif? You're not using elseif anywhere and maybe that's causing the delay.

2 answers

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.

5 comments

0

I think I messed up the script..

0

Too many elseifs.

0

Did you try it?

0

Let me edit this so It is more ordered

0

tidy*

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.