This script listens for an event whenever the player presses any of the WASD keys. However I'm having trouble getting the animation to fire. I understand that player needs to be indexed somewhere but I'm not sure how.If anyone can help me out I'd be grateful.
--Player Values and walking animations--
local already_ran = false
local grounded = false
local idleAnimation = Instance.new("Animation")
idleAnimation.AnimationId = "rbxassetid://356344446"
local Left = Instance.new("Animation")
Left.AnimationId = "rbxassetid://356869859"
local Right = Instance.new("Animation")
Right.AnimationId = "rbxassetid://356869859"
local Forward = Instance.new("Animation")
Right.AnimationId = "rbxassetid://312976450"
local Backward = Instance.new("Animation")
Right.AnimationId = "rbxassetid://312976450"
local Jump = Instance.new("Animation")
Jump.AnimationId = "rbxassetid://326922088"
function playAnimation(parent)
local AnimTrack = player.Character.Humanoid.Animator:LoadAnimation(parent) -- The issue
AnimTrack:Stop()
wait(.01)
AnimTrack:Play()
print("played anim")
end
function onGround(speed)
if grounded == false then
grounded = true
local event = game.ReplicatedStorage:WaitForChild("MovementLeftEvent")
event.OnServerEvent:connect(function(player)
if speed > 1 then
if already_ran == false then
already_ran = true
playAnimation(Left)
print("Left")
local event2 = game.ReplicatedStorage:WaitForChild("MovementRightEvent")
event2.OnServerEvent:connect(function(player)
if speed > 1 then
if already_ran == false then
already_ran = true
playAnimation(Right)
print("Right")
local event3 = game.ReplicatedStorage:WaitForChild("MovementForwardEvent")
event3.OnServerEvent:connect(function(player)
if speed > 1 then
if already_ran == false then
already_ran = true
playAnimation(Forward)
print("Forward")
local event4 = game.ReplicatedStorage:WaitForChild("MovementBackwardEvent")
event4.OnServerEvent:connect(function(player)
if speed > 1 then
if already_ran == false then
already_ran = true
playAnimation(Backward)
print("Backward")
elseif speed > 0 and already_ran == true then
already_ran = false
playAnimation(idleAnimation)
player.Character.Humanoid.Running:connect(onGround)
end
end
end)
end
end
end)
end
end
end)
end
end
end)
end
end
