When going above speed 50 a running animation plays and when below 50 it stops

this script should play a running animation i made when the player goes above 50 speed and stop below 50, it doesnt play the animation at all tho? (the speed up script is shift to slowly speed up and go faster the longer you hold it)

Location = StarterPlayerScripts

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if Character.Humanoid.Walkspeed >= 50 then
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://10628631713'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		end
		end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 49
		PlayAnim:Stop()
	end
end)

1 Like

To change the “Running animation” which Roblox has a Running AnimationID, I believe it is on the Humanoid, You can check this by test running the game and going to your character in Workspace. You would want to change the running AnimationID itself, Playing the animation on the Humanoid does not get rid of the other Moving Animation.

yea but its a really fast running animation meant to only play when going high speed, so not when normally walking too

Well I don’t see why you’re using the UserInputService then, Try using ( RunService:BindToRenderStep ) for this. And yea it’s because you’re gonna have to change the AnimationID back to the Walking one whenever they’re under 50.

Also, I believe you have to place this inside of StarterCharacterScripts for animation access. I’m not sure if it works on StarterPlayerScripts. I don’t mess around with Animations a lot…

how does the linked service work?

That’s the point of the documentation…

Just in case you dont know, walkspeed is the maximum speed a player can go, not the actual speed the player is currently moving. To find that, I suggest testing the Character.Humanoid.MoveDirection.Magnitude value to see if it is over a specific amount.

its red
image

if statement comparison is ==, not =

1 Like

its red because to check if a number is specific, you must do == instead of =

1 Like

Anyway, a good way to solve your problem is by having an Animation parented to a LocalScript inside StarterCharacterScripts, and setting the AnimationID to your animation (Make the animation priority action 1 insead of movement so it will play over roblox animations)

I should also mention that loading animations onto the Humanoid is Deprecated, and you should now load animations onto the Humanoids Animator.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = Animator:LoadAnimation(script.Animation)

local Animating = false

RunService.RenderStepped:Connect(function()
-- You can use Humanoid.MoveDirection, but it only ranges from 0 to 1 on all x y and z axis
-- However, HumanoidRoots Velocity may not be the MoveSpeed, i dont know how it converts.
if HumanoidRoot.Velocity.Magnitude >= 60 and Humanoid:GetState() == Enum.HumanoidStateType.Running and not Animating then
Animating = true
Animation:Play()
else
Animating = false
Animation:Stop()
end
end

im not sure if i did something wrong
image
image

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = Animator:LoadAnimation(script.Animation)

local Animating = false

RunService.RenderStepped:Connect(function()
	-- You can use Humanoid.MoveDirection, but it only ranges from 0 to 1 on all x y and z axis
	-- However, HumanoidRoots Velocity may not be the MoveSpeed, i dont know how it converts.
	if HumanoidRoot.Velocity.Magnitude >= 60 and Humanoid:GetState() == Enum.HumanoidStateType.Running and not Animating then
		Animating = true
		Animation:Play()
	else
		Animating = false
		Animation:Stop()
	end
end)
1 Like