Creating a Music Tool. Help pls

I am trying to create a tool that plays music and animation when it is equipped. I need the animation and sound to stop when the tool is unequipped.

This is what I tried, but it keeps me in the animation even when the tool is not equipped.

local tool = script.Parent
local sound = tool.Handle.Sound
local animation = tool.Animation

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	local AnimationTrack = Humanoid:LoadAnimation(animation)
	sound:Play()
	AnimationTrack:Play()
	
end)	
	
tool.Unequipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	local AnimationTrack = Humanoid:LoadAnimation(animation)
	sound:Stop()
	AnimationTrack:Stop()
	
end)

Try something like this. It should work, but if it doesn’t please show any errors that appear.

local tool = script.Parent
local sound = tool.Handle.Sound
local animation = tool.Animation
local AnimationTrack

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	AnimationTrack = Humanoid:LoadAnimation(animation)
	sound:Play()
	AnimationTrack:Play()
	
end)	
	
tool.Unequipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	sound:Stop()
	AnimationTrack:Stop()
	
end)

Thanks for the reply. I came up with a solution. I added a local script into the tool that controls idle animations when the tool is equipped. So it did the same thing. Here is the local script and script for sound that works.

local tool = script.Parent
local sound = tool.Handle.Sound
local animation = tool.Animation

tool.Equipped:Connect(function()
	sound:Play()
	
end)	
	
tool.Unequipped:Connect(function()
	sound:Stop()

end)
local plr = game.Players.LocalPlayer
local replicatedStorage = game:GetService("ReplicatedStorage")
local Anim2
script.Parent.Equipped:Connect(function()
	
	
	
	
		local Track2 = Instance.new("Animation")
	Track2.AnimationId = "rbxassetid://7274573741" --Idle 
		 Anim2 = plr.Character.Humanoid:LoadAnimation(Track2)
		Anim2:Play()
	
	
		
end)


script.Parent.Unequipped:Connect(function()
	
	Anim2:Stop()
	
	
	
	
	
end)

sorry its messy I butchered it super fast. I am about to get to sleep.

1 Like