I have a run script with an animation and it plays the run animations even if the player is idle while holding left shift. I want the idle animation to be playing so if someone could improve my run script to make it where it will only play the animations if left shift is being held and the player isn’t idling.
Here is my run script:
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
Character.Humanoid.WalkSpeed = 35 --run speed
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://15720702879'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim.Priority = Enum.AnimationPriority.Movement
PlayAnim:Play()
if Character.Humanoid.MoveDirection < 0 then
PlayAnim:Stop()
end
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16 --default walk speed
PlayAnim:Stop()
if Character.Humanoid.MoveDirection < 0 then
PlayAnim:Stop()
end
end
end)
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
PlayAnim:Stop()
wait(.8)
if Character.Humanoid.WalkSpeed == 35 then
PlayAnim:Play()
if Character.Humanoid.MoveDirection < 0 then
PlayAnim:Stop()
end
end
end
end)
you should check whether the player is moving using humanoid.MoveDirection.Magnitude
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and Character.Humanoid.MoveDirection.Magnitude > 0 then
--character is running, change walkspeed and play animation
end
end
end)
I wrote it here for an example. It checks for if the player is holding shift and is moving before playing the animation.
You would use Humanoid.MoveDirection.Magnitude to check whether or not the character is moving or not.
Example (If you don’t mind, I optimized your script):
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15720702879"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement
local running = false
local RUN_KEYCODE = Enum.KeyCode.LeftShift
local RUN_SPEED = 32
local WALK_SPEED = 16
local function run(runBool) -- So we don't have to repeat the same commands
if runBool then
if hum.MoveDirection.Magnitude <= 0 then return end -- If not moving then stop function execution
hum.WalkSpeed = RUN_SPEED
track:Play()
else
hum.WalkSpeed = WALK_SPEED
track:Stop()
end
running = runBool
end
local function inputBegan(input, processed)
if processed then return end
if input.KeyCode == RUN_KEYCODE then
run(true)
end
end
local function inputEnded(input, processed)
if processed then return end
if input.KeyCode == RUN_KEYCODE then
run(false)
end
end
local function jumpRequest()
run(false)
task.wait(0.8)
run(true)
end
local function moveDirectionChanged()
local magnitude = hum.MoveDirection.Magnitude
if magnitude <= 0 then
if running then
run(false)
end
end
end
userInputService.InputBegan:Connect(inputBegan)
userInputService.InputEnded:Connect(inputEnded)
userInputService.JumpRequest:Connect(jumpRequest)
hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)