Hello!
I currently have a running script for my game and I want the running animation to stop playing when the player is in the air ( Jumping or Falling ) or in water ( Swimming ).
I tried myself but I couldn’t get it to work.
This is the script. It’s placed in StarterPlayerScripts.
-- Services
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UIS: UserInputService = game:GetService("UserInputService")
-- Constants
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local Animator = Humanoid:WaitForChild("Animator")
local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed
-- Variables
local Speed: number = 26 -- Speed
local Key: Enum.KeyCode = Enum.KeyCode.LeftShift -- Key
local TweenSpeed: number = 0.2 -- FOV
local SprintingAnim = Instance.new("Animation")
SprintingAnim.AnimationId = "rbxassetid://15649645026"
local Animation = Animator:LoadAnimation(SprintingAnim)
-- Main Code
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView + (Speed / 3) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView })
UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key and Humanoid.MoveDirection.Magnitude > 0 then
Humanoid.WalkSpeed = Speed
SprintingTween:Play()
Animation:Play()
end
end
end)
UIS.InputEnded:Connect(function(Input: InputObject)
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then
Humanoid.WalkSpeed = DefaultWalkingSpeed
WalkingTween:Play()
Animation:Stop()
end
end)
-- Player Resetting
Player.CharacterAdded:Connect(function(Char)
Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
end)
to stop an animation for a specific humanoid state, you can use Humanoid.StateChanged. this will allow you to check what state the humanoid entered and stop a specific animation if it enters jumping, falling, or swimming. in the states that are not swimming/jumping/falling, you will also want to check if the animation is already playing before playing it again to ensure the animations do not get restarted.
Alright, I tried doing it myself again, and this is what I made. Although it doesn’t work, I decided to post it here to get help.
-- Services
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UIS: UserInputService = game:GetService("UserInputService")
-- Constants
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local Animator = Humanoid:WaitForChild("Animator")
local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed
-- Variables
local Speed: number = 26 -- Speed
local Key: Enum.KeyCode = Enum.KeyCode.LeftShift -- Key
local TweenSpeed: number = 0.2 -- FOV
local SprintingAnim = Instance.new("Animation")
SprintingAnim.AnimationId = "rbxassetid://15649645026"
local Animation = Animator:LoadAnimation(SprintingAnim)
local IsRunning = false
-- Main Code
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView + (Speed / 3) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView })
UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key and Humanoid.MoveDirection.Magnitude > 0 and IsRunning == false then
Humanoid.WalkSpeed = Speed
SprintingTween:Play()
Animation:Play()
IsRunning = true
end
end
end)
UIS.InputEnded:Connect(function(Input: InputObject)
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key and IsRunning == true then
Humanoid.WalkSpeed = DefaultWalkingSpeed
WalkingTween:Play()
Animation:Stop()
IsRunning = false
end
end)
local function onStateChanged(_oldState, newState)
if newState == Enum.HumanoidStateType.Jumping or newState == Enum.HumanoidStateType.Swimming or newState == Enum.HumanoidStateType.Freefall and IsRunning == true then
Animation:Stop()
IsRunning = false
Humanoid.WalkSpeed = DefaultWalkingSpeed
elseif Humanoid.MoveDirection.Magnitude > 0 then
Animation:Play()
end
end
-- Player Resetting
Player.CharacterAdded:Connect(function(Char)
Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
end)
Alright I remade again with some help from reading other Dev Forum posts. It still doesn’t work though.
Script
-- Services
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UIS: UserInputService = game:GetService("UserInputService")
-- Constants
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local Animator = Humanoid:WaitForChild("Animator")
local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed
-- Variables
local Speed: number = 26 -- Speed
local Key: Enum.KeyCode = Enum.KeyCode.LeftShift -- Key
local TweenSpeed: number = 0.2 -- FOV
local SprintingAnim = script:WaitForChild("RunningAnim")
local Animation = Animator:LoadAnimation(SprintingAnim)
local IsRunning = false
local IsJumping = false
-- Main Code
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView + (Speed / 3) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView })
UIS.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key and Humanoid.MoveDirection.Magnitude > 0 then
Humanoid.WalkSpeed = Speed
SprintingTween:Play()
Animation:Play()
end
local function onStateChanged(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping or newState == Enum.HumanoidStateType.Freefall or newState == Enum.HumanoidStateType.Swimming then
IsJumping = true
if IsJumping == true then
wait(nil)
IsRunning = false
Animation:Stop()
SprintingTween:Stop()
end
end
if newState == Enum.HumanoidStateType.Landed or newState ~= Enum.HumanoidStateType.Swimming then
IsJumping = false
if IsJumping == false and IsRunning == true then
wait(nil)
IsRunning = true
Animation:Play()
end
end
if newState == Enum.HumanoidStateType.Landed or newState == Enum.HumanoidStateType.Swimming then
IsJumping = false
if IsJumping == false and IsRunning == false then
wait(nil)
IsRunning = false
Animation:Stop()
end
end
end
end)
UIS.InputEnded:Connect(function(Input: InputObject)
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then
Humanoid.WalkSpeed = DefaultWalkingSpeed
WalkingTween:Play()
Animation:Stop()
end
end)
-- Player Resetting
Player.CharacterAdded:Connect(function(Char)
Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
end)
I’m now posting it here again so that I can hopefully get more help from other people.
Someone please, help me with it so I can see what’s wrong with my script.
-- Services
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UIS: UserInputService = game:GetService("UserInputService")
-- Constants
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local Animator = Humanoid:WaitForChild("Animator")
local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed
-- Variables
local Speed: number = 26 -- Speed
local Key: Enum.KeyCode = Enum.KeyCode.LeftShift -- Key
local TweenSpeed: number = 0.2 -- FOV
local SprintingAnim = Instance.new("Animation")
SprintingAnim.AnimationId = "rbxassetid://15649645026"
local Animation = Animator:LoadAnimation(SprintingAnim)
-- Main Code
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView + (Speed / 3) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView })
-- removed anim:play/stop on the inputbegan/ended funcs
UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key and Humanoid.MoveDirection.Magnitude > 0 then
Humanoid.WalkSpeed = Speed
SprintingTween:Play()
end
end
end)
UIS.InputEnded:Connect(function(Input: InputObject)
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then
Humanoid.WalkSpeed = DefaultWalkingSpeed
WalkingTween:Play()
end
end)
-- this is my added portion
local StateChanged
local function CheckState()
StateChanged = Humanoid.StateChanged:Connect(function(OldState,NewState)
if NewState ~= Enum.HumanoidStateType.Running and Animation.IsPlaying then
-- stop sprint anim when they are not on the ground walking and the sprint anim is playing
Animation:Stop()
elseif NewState == Enum.HumanoidStateType.Running and not Animation.IsPlaying then
Animation:Play()
end
end)
end
----
-- Player Resetting
Player.CharacterAdded:Connect(function(Char)
Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
-- my added portion, the previous event needs to be disconnected to prevent memory leaks
StateChanged:Disconnect()
CheckState()
end)
it is 2am over here so im just posting a possible solution because i forgot to follow up yesterday, basically you will hook the .StateChanged event and check to see if the player is running. if the run anim is not playing, then play it. if the character is nto in the running state and the run anim is playing, stop the run anim. it seems like the behavior you had in it was only checking for specific key inputs and such but hooking StateChanged will make this much easier for you in the long run. (you can also check .MoveDirection, but statechanged is much more reliable when done on the client and takes less execution time)