local function s(s,e)while not s:FindFirstChild(e)do s.ChildAdded:wait()end return s[e]end local e=script.Parent local n={}local T local G local X=s(script,"\083\116\097\114\116")local d=s(script,"\083\116\111\112")function PlayAnimation(s,e)e=e or 1 if n[s]then n[s]:Play(.1,1,e)else print("\067\097\110\'\116 \102\105\110\100 \097\110\105\109\097\116\105\111\110\058",s)end end function StopAnimation(s)if n[s]then n[s]:Stop()end end function ConnectAnimationToValueChange(n,T)return(s(e,n)).Changed:connect(function(s)PlayAnimation(T)end)end function LoadAnimation(s)if T then n[s.Name]=T:LoadAnimation(s)else print("\084\114\105\101\100 \116\111 \108\111\097\100 \097\110 \097\110\105\109 \119\105\116\104\111\117\116 \097 \104\117\109\097\110\111\105\100")end end function OnEquipped(n)G=e.Parent T=s(G,"\072\117\109\097\110\111\105\100")e.ChildAdded:connect(function(s)if s:IsA("\065\110\105\109\097\116\105\111\110")then LoadAnimation(animationObject)end end)for s,e in pairs(e:GetChildren())do if e:IsA("\065\110\105\109\097\116\105\111\110")then LoadAnimation(e)end end end function OnUnequipped()for s,e in pairs(n)do StopAnimation(s)end end X.OnInvoke=PlayAnimation d.OnInvoke=StopAnimation e.Equipped:connect(OnEquipped)e.Unequipped:connect(OnUnequipped)
-- Waits for the child of the specified parent
local function WaitForChild(parent, childName)
	while not parent:FindFirstChild(childName) do
		parent.ChildAdded:wait()
	end
	return parent[childName]
end

local Tool = script.Parent

local Animations = {}
local MyHumanoid
local MyCharacter

local StartAnim = WaitForChild(script, "Start")
local StopAnim = WaitForChild(script, "Stop")

function PlayAnimation(animationName, speed)
	speed = speed or 1
	if Animations[animationName] then
		Animations[animationName]:Play(0.1, 1, speed)
	else
		print("Can't find animation:", animationName)
	end
end

function StopAnimation(animationName)
	if Animations[animationName] then
		Animations[animationName]:Stop()
	end
end

function ConnectAnimationToValueChange(valueName, animationName)
	return WaitForChild(Tool, valueName).Changed:connect(function(value)
		PlayAnimation(animationName)
	end)
end

function LoadAnimation(animationObject)
	if MyHumanoid then
		Animations[animationObject.Name] = MyHumanoid:LoadAnimation(animationObject)
	else
		print("Tried to load an anim without a humanoid")
	end
end

function OnEquipped(mouse)
	MyCharacter = Tool.Parent
	MyHumanoid = WaitForChild(MyCharacter, "Humanoid")

	Tool.ChildAdded:connect(function(child)
		if child:IsA("Animation") then
			LoadAnimation(animationObject)
		end
	end)
	for _, child in pairs(Tool:GetChildren()) do
		if child:IsA("Animation") then
			LoadAnimation(child)
		end
	end
end

function OnUnequipped()
	for name, animTrack in pairs(Animations) do
		StopAnimation(name)
	end
end

StartAnim.OnInvoke = PlayAnimation
StopAnim.OnInvoke = StopAnimation

Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)
