Scripting Animations

I’m quite new to LUA scripting via Roblox, and i’ve been at this project for a while now. I’m trying to tell the script that when W, A, S, or D is held down, to stop the idle animation and play the walking animation. But I can’t seem to figure out how to call multiple KeyCodes at once, and i’m also having a problem with whenever i press any other key while the walking animation is going, it will stop?

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local mouse = game:GetService("UserInputService")

local combat = script.Parent
local anim = humanoid:LoadAnimation(combat:WaitForChild("idle"))
local walk = humanoid:LoadAnimation(combat:WaitForChild("walking"))

combat.Equipped:Connect(function()
		anim:Play()
	mouse.InputBegan:connect(function(key)
		if key.UserInputType == Enum.UserInputType.Keyboard then
			if key.KeyCode == Enum.KeyCode.W then
				anim:Stop()
				walk:Play()
			mouse.InputEnded:connect(function()
				walk:Stop()
				anim:Play()
				end)	
			end
		end
	end)
end)	
combat.Unequipped:Connect(function()
	anim:Stop()
end)

You can go in your studio and copy and paste the local script called “Animate” then paste into StarterCharacterScripts and edit your walking and idle animations as you like.

You can also tell your script to change the default walk animation when a tool is equipped. When unequipped, revert it back.

I’m having problems with the animation cancelling while pressing a random key that is not specified in the script, also just discovered that when the tool is unequipped, the animations do not stop playing.


As you can see in the video i’ve provided, while i’m holding W, i’m also clicking with my mouse and the animation has cancelled. In the end of the clip I unequip the tool and the animation is still playing.