Animation code not firing when clicking tool

Hello, This is my second ever topic here and I’m confused as to how to fix a problem I’ve run into.
I’m trying to make it so if you click with a sword tool it will do a slashing animation but when I click, the animation doesn’t trigger and it doesn’t appear to fire the code which causes a lot of problems.

Any help would be appreciated, thank you!

local sword = script.Parent
local swingAnimation = sword:WaitForChild("Animation")

local canSwing = true
local debounce = 1

script.Parent.Activated:Connect(function()
	if canSwing then
	canSwing = false
	end
	
	local character = game:GetService("Players").LocalPlayer
	local humanoid = character.Humanoid
	local animator = humanoid:WaitForChild("Animator")

	local animationTrack = humanoid:LoadAnimation(swingAnimation)
	-- Check if code is firing before play
    print("e1")
	animationTrack:Play()
    -- Check if code is firing after play
	print("e2")
end)

So, first off, your character variable is assigned to a player value, it should be a player variable first then assign a character value to playerVariable.Character or playerVariable.CharacterAdded:Wait(). Next off, are your print functions properly out-putting? And one more thing, for efficiency purposes because humanoid:LoadAnimation() is deprecated, I highly advise you change it to animator:LoadAnimation(swingAnimation. Hope that helps! If you find any other issues, don’t hesitate to reply back or just pm me. :cowboy_hat_face:

The print functions don’t appear to output at all also, I’m kind of confused as to what you mean by player value and player variable.

Thanks for the help though. :slight_smile:

Right, no worries, I’ll write my changes down below to illustrate an example for you.

local sword = script.Parent
local swingAnimation = sword:WaitForChild("Animation")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local animator = humanoid:WaitForChild("Animator")


local canSwing = true
local debounce = 1

script.Parent.Activated:Connect(function()
	if canSwing then
	canSwing = false
	end
	local animationTrack = animator:LoadAnimation(swingAnimation)
	-- Check if code is firing before play
    print("e1")
	animationTrack:Play()
    -- Check if code is firing after play
	print("e2")
end)

See if that works for you, also be sure to send a picture of your output (you can do so by going to the view tab of the active studio window and press Output) incase for any bugs.

1 Like

Place this code ,which you wrote, into your If statement.

They deprecated Humanoid:LoadAnimation()

Try using Humanoid.Animator:LoadAnimation()

I tried it and I think this is progress but the animation itself just doesn’t seem to fire.
I appreciate all of this help by the way because I haven’t been able to figure any of this out.

Okay so, I see the issue now, as of now, the Animator instance has to be manually created. To solve this issue. Create a server script in ServerScriptService and on the player’s character fully loaded, use Instance.new() to create an Animator instance and parent it to the player’s humanoid.

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
Instance.new('Animator', character.Humanoid)
end)
end)

And that should fix that issue. Also, in the sword script, re-assign the animator variable to humanoid:FindFirstChild('Animator') incase the Animator somehow doesn’t exist yet.

Ahh, I wasn’t aware of this change myself, Thanks for all of the help. :slight_smile:

1 Like

No problems, goodluck with your endeavors and happy coding! :smiley: :cowboy_hat_face:

1 Like

Sorry if I’m taking up some time but it doesn’t seem to trigger at all when I click while holding the tool.
It’s really confusing for me as I’ve never had this problem before.

I’ve run into a different problem since it was previously in a local script and I changed it to a normal script.

15:13:23.848 - Players.MasterBroNetworkPC.Backpack.The First Sword.SwordAnimation:4: attempt to index nil with ‘Character’

Normal scripts can’t see “Local Player”

Ahh, what would I do in place of local player?

Well you could get the player from the “PlayerAdded” Event but i wouldn’t recommend this
You could use Remote Events to tell the script that you want to Attack(Play the animation)
I can provide some example code if you want

So uhh, Turns out I was being dumb and forgot to re-enable the clicking functionality of the sword.
I’ve done the remote event trick and will now try to use it with the animation trigger and see how it goes.

Thank you all for the help. :smile:

I moved it back to a local script and tried it in a server script also but I ran into this error.

image

*This happens on both a server script and a local script. (Local Script is configured how it was originally)
I figured out that was due to using the rbxassetid in a string but if I use the waitForChild, it prints the message but no animation physically plays.

I have now figured out this problem and thank you everyone for your help.

@QueuedUsername, thanks for helping me to figure out the animation deprecation thing.

I’m now setting it up for the hand that holds the tool but the animation does fire correctly.

:heart: Thank you everyone for the help!

1 Like