Hi there,
I am trying to cancel out the character’s run animation in my game. I want to have a run animation play when the player’s speed is above a certain level, but it doesn’t seem to be working. I have included the scripts that I am using for reference below.
I have tried using the GetPlayingAnimationTracks
method as well as other methods, but none of them have worked. Here is the code I am using:
(ServerScriptService)
local function onCharacterAdded(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
for _, playingTrack in pairs(Animator:GetPlayingAnimationTracks()) do
playingTrack:Stop(0)
end
local AnimateScript = Character:WaitForChild("Animate")
AnimateScript.run.RunAnim.AnimationId = "rbxassetid://12007458336"
end
local function onPlayerAdded(player)
player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
(StarterPlayerScripts)
The idea was to try to implement something under the Humanoid.Running
function here to cancel the run animation and change the asset ID. It currently partly works but you have to jump to cancel out the animation, and I don’t want that to be required.
local TweenService: TweenService = game:GetService("TweenService")
local UserInputService: UserInputService = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed
local DefaultRunSpeed: number = 30
local DefaultActionKey: Enum.KeyCode = Enum.KeyCode.LeftShift
local DefaultTweenSpeed: number = 0.4
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView + (DefaultRunSpeed / 5) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView })
local AnimateScript = Character:WaitForChild("Animate")
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UserInputService: UserInputService = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed
local DefaultRunSpeed: number = 30
local DefaultActionKey: Enum.KeyCode = Enum.KeyCode.LeftShift
local DefaultTweenSpeed: number = 0.4
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView + (DefaultRunSpeed / 5) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView })
local AnimateScript = Character:WaitForChild("Animate")
Humanoid.Running:Connect(function(Speed)
local AnimationTracks = Character:WaitForChild("Humanoid"):GetPlayingAnimationTracks()
if Speed > 16 then
if AnimateScript.run.RunAnim.AnimationId ~= "rbxassetid://12007452724" then
AnimateScript.run.RunAnim.AnimationId = "rbxassetid://12007452724"
end
elseif Speed > 0 then
if AnimateScript.run.RunAnim.AnimationId ~= "rbxassetid://12007458336" then
AnimateScript.run.RunAnim.AnimationId = "rbxassetid://12007458336"
end
end
end)
UserInputService.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == DefaultActionKey then
Humanoid.WalkSpeed = DefaultRunSpeed
SprintingTween:Play()
end
end
end)
UserInputService.InputEnded:Connect(function(Input: InputObject)
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == DefaultActionKey then
Humanoid.WalkSpeed = DefaultWalkingSpeed
WalkingTween:Play()
end
end)
Player.CharacterAdded:Connect(function(Char)
Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
end)
I have also tried using the Stop
method on the Humanoid
object, but that hasn’t worked either.
I have looked for solutions on the Developer Hub, but haven’t been able to find anything that has helped.
If you have any ideas on how I can get the run animation to play correctly, please let me know. Thanks!