Animation play on tool equip

I am trying to get my animation to play when equipping my sword. I have tried a few different solutions including the one in the script below. Can anyone show me how to make the animation play.

Thanks

local animations = {"7962822434"; "7962845607"} -- Add your animation ID's here for the slashing of the sword. These will be chosen randomly every time the sword is activated.
local tool = script.Parent
local enabled = true
local char
local coolDown = .8 -- cooldown for sword activation in seconds
local swing = script.Parent.Handle.swing
local animation = script.Parent:FindFirstChild("Animation")


tool.Activated:connect(function()
animation:Play()
	if enabled then
		enabled = false
		local char = tool.Parent
		local random = animations[math.random(1,#animations)]

		local anim = Instance.new("Animation")
		anim.AnimationId = "http://www.roblox.com/asset/?id="..random

		local track = char.Humanoid:LoadAnimation(anim)
		track:Play()
		swing:Play()

		local dmg = script.Damage:Clone()
		dmg.Parent = tool.Handle
		dmg.Disabled = false
		wait(coolDown)

		enabled = true
		if dmg then
			dmg:Destroy()
		end
	end
end)

tool.Activated is invoked when the tool is used, not when equipped. If you’re trying to run code upon equip then consider using Tool.Equipped

thanks I dont know how to add tool euipped to tool activated without causing an error.

Just bind each event by itself, no need to do one inside another

local tool = -- define it

tool.Activated:Connect(function()
    -- on use
end)

tool.Equipped:Connect(function()
   -- on equip
end)
local tool = script.Parent -- define tool path
local animation = script.Parent.Animation -- define the animation path

tool.Equipped:Connect(function()
     local hum = script.Parent.Parent.Humanoid -- Only defined if the tool is in starterpack
     local track = hum:LoadAnimation(animation)
    
     track:Play()
end)

Try this. Should load animation on equipped.Make sure to define the 2 variables above.

1 Like
local animations = {"7962822434"; "7962845607"} -- Add your animation ID's here for the slashing of the sword. These will be chosen randomly every time the sword is activated.
local tool = script.Parent
local enabled = true
local char
local coolDown = .8 -- cooldown for sword activation in seconds
local swing = script.Parent.Handle.swing
local animation = script.Parent:FindFirstChild("Animation")


tool.Activated:connect(function()
local player = game.Players:GetPlayerFromCharacter(script.Parent) -- Add parent's if the script is deeper inside of the tool.

player.Character.Humanoid:LoadAnimation(animation):Play()

	if enabled then
		enabled = false
		local char = tool.Parent
		local random = animations[math.random(1,#animations)]

		local anim = Instance.new("Animation")
		anim.AnimationId = "http://www.roblox.com/asset/?id="..random

		local track = char.Humanoid:LoadAnimation(anim)
		track:Play()
		swing:Play()

		local dmg = script.Damage:Clone()
		dmg.Parent = tool.Handle
		dmg.Disabled = false
		wait(coolDown)

		enabled = true
		if dmg then
			dmg:Destroy()
		end
	end
end)

try replaceing it with

'rbxassetid'
1 Like