I have a error while trying to stop the animation if running
it doesn’t stop the animation and keeps going if I do PlayAnim:Stop()
it will just not work
here
--// ChiefTitan
local UserInputService = game:GetService("UserInputService")
local SPRINT_SPEED = 20
local player = game.Players.LocalPlayer
local Character = player.Character
local mouse = player:GetMouse()
repeat wait() until player.Character
local char = player.Character
local hum = player.Character:WaitForChild("Humanoid")
local gui = script.Parent["Run/Health"]
local fill = gui.Stamina.Fill
local sprinting = false
local stamina = 100
local canSprint = true
UserInputService.InputBegan:connect(function(key, gameProcessedEvent)
if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum and not sprinting and canSprint then
sprinting = true
hum.WalkSpeed = SPRINT_SPEED
while sprinting and stamina > 0 do
fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
stamina = stamina - 5
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://10102443434'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
wait(0.5)
end
if hum then
hum.WalkSpeed = 16
Character.Humanoid:Stop()
end
sprinting = false
end
end)
UserInputService.InputEnded:connect(function(key, gameProcessedEvent)
if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum then
sprinting = false
end
end)
while true do
if not sprinting and stamina < 100 then
stamina = stamina + 5
fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
end
if stamina >= 33 then
canSprint = true
elseif stamina == 0 then
canSprint = false
end
wait(0.5)
end
According to your InputEnded code you dont have a line of code making the animation stop, therefore the animation will keep playing until the track is told to stop.
Because you made it a local variable, it can only be used inside of the while statement. you need to define PlayAnim before the “while sprinting” loop happens.
Define the anim script before the while loop, try doing
local UserInputService = game:GetService(“UserInputService”)
local SPRINT_SPEED = 20
local player = game.Players.LocalPlayer
local Character = player.Character
local mouse = player:GetMouse()
repeat wait() until player.Character
local char = player.Character
local hum = player.Character:WaitForChild(“Humanoid”)
local gui = script.Parent[“Run/Health”]
local fill = gui.Stamina.Fill
local sprinting = false
local stamina = 100
local canSprint = true
local PlayAnim = nil
UserInputService.InputBegan:connect(function(key, gameProcessedEvent)
if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum and not sprinting and canSprint then
sprinting = true
hum.WalkSpeed = SPRINT_SPEED
while sprinting and stamina > 0 do
fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
stamina = stamina - 5
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://10102443434'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
wait(0.5)
end
if hum then
hum.WalkSpeed = 16
PlayAnim:Stop()
end
sprinting = false
end
end)
UserInputService.InputEnded:connect(function(key, gameProcessedEvent)
if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum then
sprinting = false
end
end)
while true do
if not sprinting and stamina < 100 then
stamina = stamina + 5
fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), “Out”, “Quad”, 0.5)
end
if stamina >= 33 then
canSprint = true
elseif stamina == 0 then
canSprint = false
end
wait(0.5)
end
you used “local PlayAnim” which means it was used only in that part of the function.
Before “PlayAnim:Stop()” add
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://10102443434'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
EDIT: Preferably, add the local anim before the first userInputService function, so you don’t have to type it twice, like this:
--// ChiefTitan
local UserInputService = game:GetService("UserInputService")
local SPRINT_SPEED = 20
local player = game.Players.LocalPlayer
local Character = player.Character
local mouse = player:GetMouse()
repeat wait() until player.Character
local char = player.Character
local hum = player.Character:WaitForChild("Humanoid")
local gui = script.Parent["Run/Health"]
local fill = gui.Stamina.Fill
local sprinting = false
local stamina = 100
local canSprint = true
-- add this here, remove the old one from inside uis.InputBegan
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://10102443434'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
UserInputService.InputBegan:connect(function(key, gameProcessedEvent)
if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum and not sprinting and canSprint then
sprinting = true
hum.WalkSpeed = SPRINT_SPEED
while sprinting and stamina > 0 do
fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
stamina = stamina - 5
PlayAnim:Play()
wait(0.5)
end
if hum then
hum.WalkSpeed = 16
PlayAnim:Stop()
end
sprinting = false
end
end)
UserInputService.InputEnded:connect(function(key, gameProcessedEvent)
if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.LeftShift and hum then
sprinting = false
end
end)
while true do
if not sprinting and stamina < 100 then
stamina = stamina + 5
fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
end
if stamina >= 33 then
canSprint = true
elseif stamina == 0 then
canSprint = false
end
wait(0.5)
end