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.
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.
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.
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.
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’
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.
I moved it back to a local script and tried it in a server script also but I ran into this error.
*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.