The problem is on the line 47 (humanoid.WalkSpeed = “run”). When the shift is pressed the game stops rendering and I get Call stack “message”, and I don’t even know what that means. Output doesn’t tell me anything about it.
local UserInputService = game:GetService("UserInputService")
local Player = script.Parent
local humanoid = Player:WaitForChild("Humanoid")
--load animations--
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://5450777900"
local walkAnim = humanoid:LoadAnimation(animation)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://4593960224"
local trotAnim = humanoid:LoadAnimation(animation)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://4576387446"
local runAnim = humanoid:LoadAnimation(animation)
--modes and velocity--
local mode = "walk"
local BMod = "walk"
local BVel = 3
local anim = walkAnim
local BAnim = walkAnim
--functions--
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftControl then --toggle trotting
if mode == "walk" then
mode = "trot"
humanoid.WalkSpeed = 15
anim = trotAnim
BAnim = trotAnim
BVel = 15
BMod = "trot"
else
mode = "walk"
humanoid.WalkSpeed = 3
anim = walkAnim
BAnim = walkAnim
BVel = 3
BMod = "walk"
end
elseif key.KeyCode == Enum.KeyCode.LeftShift then --toggle running
if mode ~= "run" then
mode = "run"
humanoid.WalkSpeed = 20
anim = runAnim
else
mode = BMod
humanoid.WalkSpeed = BVel
anim = BAnim
end
end
if key.KeyCode == Enum.KeyCode.W then
anim:Play()
end
end)
UserInputService.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.W then
local ActiveTracks = humanoid:GetPlayingAnimationTracks()
for _,v in pairs(ActiveTracks) do
v:Stop()
end
end
end)
I’m trying to create 3-moving states: walk, trot, run. Trot and walk are working fine. Anyways I feel like there is overall better way to do this, so all the suggestions are welcome. Thank you.