I got help to make a toggle sprint system, and I want a sprint animation to play when the character moves. It works but after the animation starts it won’t stop.
local character = script.Parent
local Anim = Instance.new('Animation')
local humanoid = character:WaitForChild("Humanoid")
local IsRunning = false
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
IsRunning = not IsRunning
if IsRunning == true then
Character.Humanoid.WalkSpeed = 35
humanoid.Running:Connect(function(speed)
if speed >= 30 then
Anim.AnimationId = 'rbxassetid://5362333091'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
if speed <= 1 then
PlayAnim:Stop()
end
end
end)
else
Character.Humanoid.WalkSpeed = 15
PlayAnim:Stop()
end
end
end)
If the character’s speed is originally 15 why set the speed to check if it is less than 1? The script wouldn’t be able to process what is going on if the original speed is 15 but it is reading for 1.
Wait I see what you’re saying can you send a video of what is going on?
I found your error. In the >= 30 check, you put the <= 1 inside it which it never reaches because its >= 30.
Here is the fixed code:
local character = script.Parent
local Anim = Instance.new('Animation')
local humanoid = character:WaitForChild("Humanoid")
local IsRunning = false
local connection = false
UIS.InputBegan:Connect(function(input, gpe)
if input.KeyCode == Enum.KeyCode.LeftControl and not gpe then
IsRunning = not IsRunning
if IsRunning then
humanoid.WalkSpeed = 35
connection = humanoid.Running:Connect(function(speed)
if speed >= 30 then
Anim.AnimationId = 'rbxassetid://5362333091'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
elseif speed <= 1 then
PlayAnim:Stop()
end
end)
else
humanoid.WalkSpeed = 15
PlayAnim:Stop()
if connection then
connection:Disconnect()
end
end
end
end)
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local character = script.Parent
local Anim = Instance.new('Animation')
local humanoid = character:WaitForChild("Humanoid")
local IsRunning = false
local connection = false
UIS.InputBegan:Connect(function(input, gpe)
if input.KeyCode == Enum.KeyCode.LeftControl and not gpe then
IsRunning = not IsRunning
if IsRunning then
humanoid.WalkSpeed = 35
connection = humanoid.Running:Connect(function(speed)
if speed >= 30 then
Anim.AnimationId = 'rbxassetid://5362333091'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
elseif speed <= 1 then
PlayAnim:Stop()
wait(0.1)
PlayAnim:Stop()
end
end)
else
humanoid.WalkSpeed = 15
PlayAnim:Stop()
if connection then
connection:Disconnect()
end
end
end
end)