There are two issues with the animation: First, it doesn’t play when I start running from Idle. Second, it doesn’t stop when I stand still while holding Shift.
Here is the code
local plr = game:GetService("Players").LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humanoid = chr:WaitForChild("Humanoid")
local tween = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local runanim = script.Parent.Animate.run.RunAnim
local track = humanoid:LoadAnimation(runanim)
local cam = workspace.CurrentCamera
UIS.InputBegan:Connect(function(key, gameprocessed)
if gameprocessed then return end
if key.KeyCode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = 30
tween:Create(cam, TweenInfo.new(1), {FieldOfView = 80}):Play()
if humanoid.MoveDirection.Magnitude > 0 then
track:Play()
else
track:Stop()
end
end
end)
UIS.InputEnded:Connect(function(key, gameprocessed)
if gameprocessed then return end
if key.KeyCode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = 16
tween:Create(cam, TweenInfo.new(1), {FieldOfView = 70}):Play()
track:Stop()
end
end)
UserInputService.InputBegan is only fired whenever the input is processed. It does not run every frame or anything of that sort.
Therefore,
will only run once whenever you press Shift.
You could instead store the run state in a variable, to which I will from now on refer to as isRunning. You would update it from within UserInputService.InputBegan and UserInputService.InputEnded.
You could listen to RunService.PreAnimation (Important that it’s done outside of the input connection callbacks!! You’ll create a memory leak otherwise), from which you would check isRunning and the move direction magnitude to determine whether the animation should be played or not.
From what I’m seeing in the attached video, this issue is specifically caused by what I brought up. While you are correct that this COULD theoretically, in other cases, be caused by incorrectly set up animation priorities, that does not seem to be the issue here.