You can write your topic however you want, but you need to answer these questions:
-
I want to make my sprint animation stop if the character is not running.
-
I have a sprint script that plays an animation and once it plays the animation and I stop running the animation still goes on.
- I have tried checking the MoveDirection Magniuted to make it stop but it did not work while the animation was playing
local mouse = game.Players.LocalPlayer:GetMouse()
local Player = game.Players.LocalPlayer
local running = false
local Character = Player.Character
function getTool()
for _, kid in ipairs(script.Parent:GetChildren()) do
if kid.className == "Tool" then return kid end
end
return nil
end
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://6773506019'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
local AnimationTracks = Character.Humanoid:GetPlayingAnimationTracks()
mouse.KeyDown:connect(function (key) -- Run function
key = string.lower(key)
if string.byte(key) == 48 then
running = true
local keyConnection = mouse.KeyUp:connect(function (key)
if string.byte(key) == 48 then
running = false
end
end)
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
if Character.Humanoid.MoveDirection.Magnitude > 0 then
PlayAnim:Play()
else
PlayAnim:Stop()
end
repeat wait () until running == false
keyConnection:disconnect()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
PlayAnim:Stop()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
A couple things
-
The Mouse Object is considered “Legacy”, I’d prefer using UserInputService
-
Humanoid:LoadAnimation
is considered deprecated, do change to the Animator
object instead
Here’s hopefully a much better version of your script:
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Cam = workspace.CurrentCamera
function getTool()
for _, kid in ipairs(script.Parent:GetChildren()) do
if kid.className == "Tool" then return kid end
end
return nil
end
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://6773506019'
local PlayAnim = Humanoid.Animator:LoadAnimation(Anim)
local AnimationTracks = Humanoid.Animator:GetPlayingAnimationTracks()
UIS.InputBegan:Connect(function(Input, Chatted)
if Chatted then
return
end
if Humanoid.MoveDirection.Magnitude > 0 and Input.KeyCode == Enum.KeyCode then --Change this to what KeyCode you want to detect
Humanoid.WalkSpeed = 30
PlayAnim:Play()
for Loop = 1, 5 do
Cam.FieldOfView = (70 + (Loop * 2))
wait()
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode then--Same thing
Humanoid.WalkSpeed = 16
PlayAnim:Stop()
for Loop = 1, 5 do
Cam.FieldOfView = (70 - (Loop * 2))
wait()
end
end
end)
2 Likes
the only problem I have is that while pressing shift if I stop moving and keep on pressing shift the animation still plays.
so like if I start sprinting and don’t release shift it will countinue playing the animation
Oh I see
Then you can use another event called Humanoid.Running
to detect when the Humanoid stops running or starts walking, just add this into your script along with your other events:
Humanoid.Running:Connect(function(CurrentSpeed)
if CurrentSpeed > 0 then
Humanoid.WalkSpeed = 30
PlayAnim:Play()
for Loop = 1, 5 do
Cam.FieldOfView = (70 + (Loop * 2))
wait()
end
else
Humanoid.WalkSpeed = 16
PlayAnim:Stop()
for Loop = 1, 5 do
Cam.FieldOfView = (70 - (Loop * 2))
wait()
end
end
end)
thank you very much man!! by using a combination of my knowledge and your help I was able to make a script that worked. Cheers!