Im trying to make the animation change from walking to running when the player walkspeed is 30 but I keep getting this error:
LoadAnimation requires an Animation object
The object is an animation, it has keyframes so that counts as animations right?
This is the code:
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Keys = {}
local SpeedToChange = 0.05
local MaxSpeed = 30
local InitialSpeed = Humanoid.WalkSpeed
local WKey = Enum.KeyCode.W
local SKey = Enum.KeyCode.S
local AKey = Enum.KeyCode.A
local DKey = Enum.KeyCode.D
local RunAnim = script.RunAnim
local RunPlay = Humanoid.Animator:LoadAnimation(RunAnim)
UIS.InputBegan:Connect(function(Input, GameProcessed)
if GameProcessed then return end
local Keycode = Input.KeyCode
if Keycode == WKey or Keycode == AKey or Keycode == SKey or Keycode == DKey then
table.insert(Keys, Keycode)
end
end)
UIS.InputEnded:Connect(function(Input, GameProcessed)
if GameProcessed then return end
local Keycode = Input.KeyCode
if Keycode == WKey or Keycode == AKey or Keycode == SKey or Keycode == DKey then
for i, v in Keys do
if v == Keycode then
table.remove(Keys, i)
break
end
end
end
end)
RunService.Heartbeat:Connect(function()
if #Keys ~= 0 then
Humanoid.WalkSpeed = math.min(Humanoid.WalkSpeed + SpeedToChange, MaxSpeed)
else
Humanoid.WalkSpeed = math.max(Humanoid.WalkSpeed - (SpeedToChange*3), InitialSpeed)
end
if Humanoid.WalkSpeed == MaxSpeed then
RunPlay:Play()
else
RunPlay:Stop()
end
end)