How To Stop Sound When Tool Unequipped?

  1. What do you want to achieve? When my tool is Unequipped the sound will stop.

  2. What is the issue? I dont know how to stop the sound.
    My script

script.Parent.Equipped:Connect(function()
		local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
		local sound = Instance.new("Sound",Hum.Torso)
		sound.SoundId = "rbxassetid://9849884556"
		sound.Volume = 0.025
		sound.Looped = true
		sound:Play()
	end)

script.Parent.Unequipped:Connect(function()
	local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
	--The problem is here, the script doesn't find the sound.
end)
  1. What solutions have you tried so far? I tried to put an Sound:Remove but it din’t work.
script.Parent.Unequipped:Connect(function()
	local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
	Humanoid.Torso:FindFirstChild("Sound"):Stop()
end)

Utilize this: Sound:Stop

1 Like

It din’t worked, the sound just continues.

Humanoid.Parent.Torso not the code you just wrote.

Perhaps making the sound a global variable instead of a local one will help?
Not sure if this will work but let me know if it did.

script.Parent.Equipped:Connect(function()
		local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
		sound = Instance.new("Sound",Hum.Torso)
		sound.SoundId = "rbxassetid://9849884556"
		sound.Volume = 0.025
		sound.Looped = true
		sound:Play()
	end)

script.Parent.Unequipped:Connect(function()
	local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
	Sound:Stop() -- or you could use Sound:Destroy() if you want the sound completely gone
end)
1 Like

Avoid using globals, if you must, declare upvalues/non-locals instead, i.e;

local sound = Instance.new("Sound")

local function onEquipped()
	--'sound' is captured by this closure
end

local function onUnequipped()
	--'sound' is captured by this closure
end

I meant sound:Stop() not with a capital S my bad

just do this. I put the unequipped function inside equipped so it knows what sound is.
it just destroys it and this is good so multiple sounds don’t go into your hums torso.

What bloxy said is correct. Here is what the fixed code would look like. If you go into the explorer menu you’ll see that the humanoid does not have a Torso child, but that the Torso is part of the character. The humanoid was not needed in this case.

script.Parent.Equipped:Connect(function()
  local sound = Instance.new("Sound",script.Parent.Parent.Torso)
  sound.SoundId = "rbxassetid://9849884556"
  sound.Volume = 0.025
  sound.Looped = true
  sound:Play()
end)

script.Parent.Unequipped:Connect(function()
	 script.Parent.Parent.Torso.Sound:Stop()
end)

Edit: You might want to destroy it instead of just stopping it, like @SutartDraws mentioned.

1 Like

Good point, global variables would not be ideal but it could be used as a temporarily solution

And that’s what I just said…

I tried to make him realize his own fault by himself, but you just answered…

my apologies! tried to help out by adjusting his full script for him.

1 Like

No problem. Glad you helped anyways!

1 Like