Running, walking and jumping animations don't play the first time they're suppsed to but work fine after

  1. What do you want to achieve? Keep it simple and clear!
    Make the corresponding animations play when the player presses / releases shift or jumps along with tweening the player’s walkspeed and fov.

  2. What is the issue? Include screenshots / videos if possible!
    The animations don’t play the first time the player presses shift or jumps after loading in but work fine after the player releases shift. (The tweens work though.)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried defining the animations at the start of the script, however that broke the entire thing. I also tried making them local and vice versa and defining the individual animations as Anim1, 2 and 3.

local Player = game.Players.LocalPlayer or script.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character.Humanoid or Character:WaitForChild("Humanoid")

local Camera = game.Workspace.CurrentCamera

local UserInputService = game:GetService("UserInputService")

local CanSprint, CanWalk = true, true

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if Humanoid.MoveDirection.Magnitude > 0 then
			RunningAnimation = Humanoid:LoadAnimation(script.Animations.Sprint)
			local Properties = {WalkSpeed = 24}
			local Info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
			local PlayerTween = game:GetService("TweenService"):Create(Humanoid, Info, Properties)
			PlayerTween:Play()
			local Properties = {FieldOfView = 90}
			local Info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
			local CameraTween = game:GetService("TweenService"):Create(Camera, Info, Properties)
			CameraTween:Play()
			WalkingAnimation:Stop(1)
			if CanSprint then
				RunningAnimation:Play(1)
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		WalkingAnimation = Humanoid:LoadAnimation(script.Animations.Walk)
		local Properties = {WalkSpeed = 8}
		local Info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
		local PlayerTween = game:GetService("TweenService"):Create(Humanoid, Info, Properties)
		PlayerTween:Play()
		local Properties = {FieldOfView = 70}
		local Info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
		local CameraTween = game:GetService("TweenService"):Create(Camera, Info, Properties)
		CameraTween:Play()
		RunningAnimation:Stop(1)
		if CanWalk then
			WalkingAnimation:Play(1)
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if Humanoid.MoveDirection.Magnitude == 0 then
		local Properties = {WalkSpeed = 8}
		local Info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
		local PlayerTween = game:GetService("TweenService"):Create(Humanoid, Info, Properties)
		PlayerTween:Play()
		local Properties = {FieldOfView = 70}
		local Info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
		local CameraTween = game:GetService("TweenService"):Create(Camera, Info, Properties)
		CameraTween:Play()
		RunningAnimation:Stop(1)
		WalkingAnimation:Stop(1)
	end
end)

Humanoid.Jumping:Connect(function(Jumping)
	if Jumping then
		JumpingAnimation = Humanoid:LoadAnimation(script.Animations.Jump)
		RunningAnimation:Stop(0.25)
		WalkingAnimation:Stop(0.25)
		JumpingAnimation:Play(0.25)
		CanSprint = false
		CanWalk = false
	end
end)

Humanoid.StateChanged:Connect(function(OldState, NewState)
	if NewState == Enum.HumanoidStateType.Landed then
		CanSprint = true
		CanWalk = true
		local function IsShiftKeyDown()
			return UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)
		end	
		JumpingAnimation:Stop(0.25)
		if IsShiftKeyDown() then
			RunningAnimation:Play(0.25)
		else
			WalkingAnimation:Play(0.25)
		end	
	end
end)

UserInputService.InputBegan:Connect(function()
	if Humanoid.MoveDirection.Magnitude == 0 then
		local function IsShiftKeyDown()
			return UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)
		end
		if not IsShiftKeyDown() then
			WalkingAnimation:Play(1)
		end
	end
end)

local function OnSpawn()
	print("Player Spawned")
end

Also, i know this entire thing is probably a huge mess, i’m pretty new so i don’t really know about any better ways lol

1 Like

You may need to perform a quick poll before playing the animation.

if Track.Length <= 0 then repeat task.wait() until Track.Length > 0 end --Don't poll if already loaded.
Track:Play()

Load all your animations before the UserInputService functions so they are ready to use.
The time it takes to load every animation every time someone inputs a command will affect your game.