Hi, I'm a beginner game developer and I need help with a movement animation issue. I’ve created custom animations for walking and sprinting. When I start the game, the walking animation works correctly. When I press Shift, the sprint animation also plays as intended.
However, after sprinting, the walking animation becomes glitched or stops working properly until I reset the character or the game.
To change the default walking animation, I modified the existing animation script in the game. For sprinting, I created a new script that looks like this:
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local runningAnimation = Instance.new("Animation")
runningAnimation.AnimationId = "rbxassetid://136014078144391"
local runningTrack = humanoid:LoadAnimation(runningAnimation)
local walkSpeed = 10
local runSpeed = 25
local isRunning = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then
isRunning = true
humanoid.WalkSpeed = runSpeed
end
end)
UIS.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.LeftShift then
isRunning = false
humanoid.WalkSpeed = walkSpeed
end
end)
RunService.RenderStepped:Connect(function()
local velocity = humanoidRootPart.Velocity
if isRunning == true and velocity.Magnitude > 1 then
if not runningTrack.IsPlaying then
runningTrack:Play()
end
else
if runningTrack.IsPlaying then
runningTrack:Stop()
end
end
end)
I would appreciate it if someone could help me
** You are now Level 1! **