Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isnt my character preforming the animation when clicking it?

I'm making a sword, and I want it so that when you equip the tool and click it swings the sword through the animation i made. Why wont it work?

tool = script.Parent
local char = script.Parent.Parent.Parent.Character

tool.Activated:connect(function()
	local char = tool.Parent
	local hum = char.Humanoid
	
	hum:LoadAnimation(script.Parent.Animation):Play()
end)

3 answers

✓ Accepted answer

A few things you can do:

  1. Make sure the animation has Looping FALSE and Priority as ACTION so it overlaps any other animation
  1. Get the Character the safer way, it's hard to tell if script.Parent.Parent.Parent.Parent is whatever
  1. Avoid Global Variables for game performance

Here:

local Tool = script.Parent
local plr = game.Players.LocalPlayer -- local script only
local Char = plr.Character or plr.CharacterAdded:Wait() -- Character

Tool.Activated:Connect(function() -- Connect not connect, connect is deprecated
    local hum = Char:WaitForChild('Humanoid')
	local LoadIt = hum:LoadAnimation(script.Parent.Animation)
	LoadIt:Play()
end)

Now it should work :D!

Hopefully this helped!

3 comments

0

thanks! How do I make the animation faster?

0

By changing the time

0

Animation Editor

You messed up on line 8. The function LoadAnimation returns an AnimationTrack so you need to create the reference to the new AnimationTrack first then use Play() on it.

tool = script.Parent
local char = script.Parent.Parent.Parent.Character

tool.Activated:connect(function()
    local char = tool.Parent
    local hum = char.Humanoid

    local animationTrack = hum:LoadAnimation(script.Parent.Animation)
    -- Then play the animation track to run the animation you used in the arguments
    animationTrack:Play()
end)

Try this

local tool = script.Parent
local animation = nil


tool.Equipped:Connect(function()
	local char = script.Parent.Character
	local humanoid = char:WaitForChild("Humanoid")
	
	animation = humanoid:LoadAnimation(script.Parent.Animation)
end)

tool.Activated:connect(function()
  	animation:Play()
end)

tool.UnEquipped:Connect(function()
	animation:Stop()
	animation = nil
end)

That may work

1 comment

0

how can I make the animation faster?

This discussion is preserved as it was. New answers, comments, and votes are closed.