UIS = game:GetService("UserInputService")
plr = game.Players.LocalPlayer
print(plr)
plr.CharacterAdded:Connect(function(Character)
local Char = Character
local Animator = Char:WaitForChild("Humanoid"):FindFirstChild("Animator")
local Run2 = Instance.new("Animation")
Run2.AnimationId = "rbxassetid://7422019829"
Run2.Parent = Char
local RunTrack2 = Animator:LoadAnimation(Run2)
RunTrack2.Looped = true
local Running = false
UIS.InputBegan:Connect(function(Key)
print("Keyboard User")
Key = Key.KeyCode
if Key == Enum.KeyCode.LeftShift then
if Running == false then
Running = true
print("Started running")
end
repeat
Char.Humanoid.WalkSpeed = Char.Humanoid.WalkSpeed + 2
print(Char.Humanoid.WalkSpeed)
wait(0.2)
until Char.Humanoid.WalkSpeed == 50 or not Running
end
UIS.InputEnded:Connect(function(Ekey)
if Ekey.KeyCode == Enum.KeyCode.LeftShift then
if Running == true then
Running = false
end
repeat
Char.Humanoid.WalkSpeed = Char.Humanoid.WalkSpeed - 5
print(Char.Humanoid.WalkSpeed)
wait(0.1)
until Char.Humanoid.WalkSpeed <= 16 or Char.Humanoid.WalkSpeed == 16 or Running
if Char.Humanoid.WalkSpeed <= 16 then
Char.Humanoid.WalkSpeed = 16
end
end
end)
end)
end)
Plan: I want to play an animation when the player reaches a walk speed of >30, and future animations for higher speeds (this script is going to be expanded soon so yeah)
Issue: I can’t play it in the same loop as the speed increase, or the animation would spam and i can’t play it after because the loop would still be running, preventing the rest of the script from working
the only solution i’ve heard is Coroutines but i don’t get the examples / explanations on the developer hub.
if there’s an easier solution please tell me! if not then can someone help explain how i can use coroutine in this instance? i’m not asking someone to really fix my script for me, but rather to help me with understanding the method used so i can fix it if that makes sense