I'm trying to make my script change the animations for the player when they move, but can't figure out how I would check if the player stopped pressing keys and make the idle animation play.
local player = game.Players.LocalPlayer
local character = player.Character
local UserInputService = game:GetService("UserInputService")
if not character or character.Parent then
repeat
wait()
until character
end
local humanoid = character:WaitForChild("Humanoid")
humanoid.WalkSpeed = 12
local animationTrack = Instance.new("Animation")
local running = false
local currentAction = nil
local animations = {["walk"] = "rbxassetid://4867116428",
["run"] = "rbxassetid://4879512283",
["idle"] = "rbxassetid://4867149545",
["jump"] = "rbxassetid://04879987572",
["climb"] = "rbxassetid://180436334"}
animationTrack.AnimationId = animations["idle"]
local animation = humanoid:LoadAnimation(animationTrack)
animation:Play()
local movementKeys = {Enum.KeyCode.W,Enum.KeyCode.A,Enum.KeyCode.S,Enum.KeyCode.D}
local function playAnimation(input, gameProccessed)
if gameProccessed == false then
if input.UserInputType == Enum.UserInputType.Keyboard then
for i=1,#movementKeys do
if input.KeyCode == movementKeys[i] then
if running == false then
animationTrack.AnimationId = animations["walk"]
local animation = humanoid:LoadAnimation(animationTrack)
animation:Play()
else
animationTrack.AnimationId = animations["run"]
local animation = humanoid:LoadAnimation(animationTrack)
animation:Play()
end
end
end
if input.KeyCode == Enum.KeyCode.Space then
animationTrack.AnimationId = animations["jump"]
local animation = humanoid:LoadAnimation(animationTrack)
currentAction = "jump"
animation:Play()
end
if input.KeyCode == Enum.KeyCode.LeftShift then
animationTrack.AnimationId = animations["run"]
local animation = humanoid:LoadAnimation(animationTrack)
animation.Name = "run"
animation:Play(0.5)
humanoid.WalkSpeed = 30
running = true
end
end
end
end
local function stopAnimation(input, gameProccessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.LeftShift then
for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do
if v.Name == "run" then
v:Stop(0.5)
end
end
humanoid.WalkSpeed = 12
running = false
end
end
end
UserInputService.InputBegan:Connect(playAnimation)
UserInputService.InputEnded:Connect(stopAnimation)

1 comment
You can simply attach a Listener to the Idle HumanoidStateType.