I’m making a script where a sword is pulled out and plays an idle animation. If you press a key (W) then the animation will stop. I’ll make a running animation play when the idle one stops. But, I don’t get why the animation won’t stop when I press W. (there’s going to be more stopAnimKeys but for testing I’m just using W. The animation showing up and going turning off when I equip and unequip works, just not the W part.
Here’s the code:
local tool = script.Parent
local Animation = script:WaitForChild("Idle")
local userInput = game:GetService("UserInputService")
local stopAnimKeys = Enum.KeyCode.W
tool.Equipped:Connect(function()
local Char = tool.Parent
if Char then
local Humanoid = Char:FindFirstChildWhichIsA("Humanoid")
if Humanoid then
local LoadedAnim = nil
if Humanoid.RigType == Enum.HumanoidRigType.R6 and Animation then
LoadedAnim = Humanoid:LoadAnimation(Animation)
end
if LoadedAnim then
LoadedAnim:Play()
end
tool.Unequipped:Connect(function()
LoadedAnim:Stop()
end)
end
end
end)
local Char = tool.Parent
local Humanoid = Char:FindFirstChildWhichIsA("Humanoid")
local LoadedAnim = Humanoid:LoadAnimation(Animation)
userInput.InputBegan:Connect(function(input)
if input.KeyCode == stopAnimKeys then
LoadedAnim:Stop()
end
end)
The formatting isn’t great as I’m not good at posting on DevForum, but hopefully it’s understandable.
Edit: Here’s a screenshot just in case:
Can you show us the Error that is showing in the Output?
It’s probably because LoadedAnim
is assigned to a temporary AnimationTrack. You will have to update it when needed.
“Players.Batmanissupercoolbro.Backpack.Sword.AnimationScript:33: attempt to index nil with ‘LoadAnimation’ - Server - AnimationScript:33”
Sorry, I’m new to coding so I have no clue what that means lol
This is should be inside a LocalScript not a Server Script. You also repeated the LoadAnimation variable, It should only repeated once.
Ok, so the animation shows up on the LocalScript so that works, but how can I replace stuff to make sure I don’t use LoadAnimation twice so that it works? Also getting the same error, so I see what you mean with the LoadAnimation variable thing.
I think remove the code at line 33 and 32 should work
Try using this script.
local tool = script.Parent
local Animation = script:WaitForChild("Idle")
local userInput = game:GetService("UserInputService")
local stopAnimKeys = Enum.KeyCode.W
local Char = tool.Parent
local Humanoid = Char:FindFirstClassWhichIsA("Humanoid")
local LoadedAnim = Humanoid:LoadAnimation(Animation)
tool.Equipped:Connect(function()
if Char then
if Humanoid then
if LoadedAnim then
LoadedAnim:Play()
end
tool.Unequipped:Connect(function()
LoadedAnim:Stop()
end)
end
end
end)
userInput.InputBegan:Connect(function(input)
if input.KeyCode == stopAnimKeys then
LoadedAnim:Stop()
end
end)
Now even the idle animation doesn’t show up
I’m getting an error: “FindFirstClassWhichIsA is not a valid member of Backpack “Players.Batmanissupercoolbro.Backpack” - Client - AnimationScript:13”
Indeed, because FindFirstClassWhichIsA
doesn’t exist. Swap Class
with Child
.
Did that, now at 14 it is an “attempt to index nil with ‘LoadAnimation’”
ohhh ok, Now i think this should work
local tool = script.Parent
local Animation = script:WaitForChild("Idle")
local userInput = game:GetService("UserInputService")
local stopAnimKeys = Enum.KeyCode.W
local LoadedAnim
tool.Equipped:Connect(function()
local Char = tool.Parent
if Char then
local Humanoid = Char:WaitForChild("Humanoid")
if Humanoid then
if Humanoid.RigType == Enum.HumanoidRigType.R6 and Animation then
LoadedAnim = Humanoid:LoadAnimation(Animation)
end
if LoadedAnim then
LoadedAnim:Play()
end
end
end
end)
local Char = tool.Parent
userInput.InputBegan:Connect(function(input)
if input.KeyCode == stopAnimKeys then
LoadedAnim:Stop()
end
end)
1 Like
That worked! Thanks! Now all I need to do is make it so when I’m not pressing W, the Idle animation plays.
1 Like
So for some reason, adding “and Enum.KeyCode.A” / S / D completely ruins everything what do I change? “or” doesn’t work either.
Can you create new post and include script in it? so I help you and everyone who had this issue too because this post was solved
1 Like
Made a new post about it! Hopefully other people will be able to see it and understand.
1 Like