My animation is working really weirdly.
I am clueless to what’s going on.
I can explain it as the sprinting animation playing in tandem with the default walking animation.
Here’s a video:
robloxapp-20240627-2115569.wmv (1.7 MB)
And, here’s the script.
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
-- Variables
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character and Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
-- Constants
local CommonVariables = {}
CommonVariables.SprintingKeyCode = Enum.KeyCode.LeftShift
CommonVariables.DefaultHumanoidWalkSpeed = 12
CommonVariables.DefaultCameraFieldOfView = 70 -- // OPTIONAL
CommonVariables.CurrentWalkSpeed = CommonVariables.DefaultHumanoidWalkSpeed
CommonVariables.RunningMultipiler = 2.5
CommonVariables.SprintingAlpha = 0.1
local SprintAnim = Humanoid.Animator:LoadAnimation(script:WaitForChild("PlayerSprint"))
local Sprinting = false
-- Functions
local function UpdatedLocalVariables(newCharacter)
Humanoid = newCharacter:WaitForChild("Humanoid")
Character = newCharacter
end
local function Lerp(Start, End, Alpha) : number
return Start + (End - Start) * Alpha
end
local function IsActorDied()
if Humanoid then
if Humanoid.Health <= 0 then
return true
else
return false
end
else
warn(("[%s]: Humanoid not found!"):format(script.Name))
return true
end
end
local function RenderStepped(Deltatime)
local ActorDied = IsActorDied()
if not ActorDied and Character then
if UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) and Humanoid.MoveDirection.Magnitude > 0 then
local Sprinting = true
CommonVariables.CurrentWalkSpeed = Lerp(
CommonVariables.CurrentWalkSpeed,
CommonVariables.DefaultHumanoidWalkSpeed * CommonVariables.RunningMultipiler,
CommonVariables.SprintingAlpha
)
SprintAnim:Play()
else
local Sprinting = false
CommonVariables.CurrentWalkSpeed = Lerp(
CommonVariables.CurrentWalkSpeed,
CommonVariables.DefaultHumanoidWalkSpeed,
CommonVariables.SprintingAlpha
)
SprintAnim:Stop()
end
else
local Sprinting = false
CommonVariables.CurrentWalkSpeed = Lerp(
CommonVariables.CurrentWalkSpeed,
CommonVariables.DefaultHumanoidWalkSpeed,
CommonVariables.SprintingAlpha
)
SprintAnim:Stop()
end
-- Other Stuff
Camera.FieldOfView = Lerp(
Camera.FieldOfView,
(UserInputService:IsKeyDown(CommonVariables.SprintingKeyCode) and CommonVariables.DefaultCameraFieldOfView * 1.25 or CommonVariables.DefaultCameraFieldOfView),
0.05
)
Humanoid.WalkSpeed = CommonVariables.CurrentWalkSpeed
end
RunService.RenderStepped:Connect(RenderStepped)
Player.CharacterAdded:Connect(UpdatedLocalVariables)
edit: the sprint animation’s priority is higher too