Stopping music when Tool Unequipped

I want to be able to stop the music when the Boombox is unequipped.

However, I am keep on getting this error:
https://gyazo.com/cbfdbd3a9780733439fb2e9e4785692d

Tool Setup:
https://gyazo.com/c809de521ae559888b047a05ff97cbfd

Local Script:

script.Parent.Unequipped:Connect(function()
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("RadioUI") then 
		game.Players.LocalPlayer.PlayerGui.RadioUI:Destroy()
	else
		warn("No GUI to Destroy.")
	end
	
	local Music = script.Parent.Handle.Music
	game.ReplicatedStorage.StopSoundEvent:FireServer(Music)
end)

Server Script:

game.ReplicatedStorage.StopSoundEvent.OnServerEvent:Connect(function(Player, Music)
	Music:Stop()
end)

I must be really dumb right now, if you could help me out that would be great.

What is “SoundScript”? The first script has 10-11 lines, and the 2nd one is 3 lines

1 Like

Is that your full script? It seems odd to me you would be getting a workspace error even though I don’t see workspace written in the script anywhere.
A few things to try:

  • If this isn’t your full script, then wherever you have workspace written, it looks like you wrote it as Workspace. I’m pretty sure that’s deprecated, so try workspace with a lowercase w instead.
  • If that was your full script then I think it might be because when you equip a tool, the tool gets transferred from the backpack in the player from game.Players into the character in workspace. When you unequip, however, it goes from the character back into the backpack. You might have to change some part of your script so it refers to the backpack, not the character. In the error you have, it looks like the boombox was transferred and so when the script tried looking for it in the character, it didn’t find it. I’m not sure which part of the script it is, but I think it’s that.
  • If you’re still getting weird errors, and the above didn’t help, then try debugging using prints.
script.Parent.Unequipped:Connect(function()
print("Unequipped")
print(script.Parent.Name)--This tells you whether or not it's in the backpack or the actual character.
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("RadioUI") then
print("FoundRadioUI") 
		game.Players.LocalPlayer.PlayerGui.RadioUI:Destroy()
print("RadioDestroyed")
	else
		warn("No GUI to Destroy.")
	end
	
	local Music = script.Parent.Handle.Music
if Music ~= nil then
print("MusicExists")
	game.ReplicatedStorage.StopSoundEvent:FireServer(Music)
end
end)

And then for the server script:

game.ReplicatedStorage.StopSoundEvent.OnServerEvent:Connect(function(Player, Music)
print("EventFired")
	Music:Stop()
print("MusicStopped")
end)
1 Like

This is the sound script ^^^

Full Local Script:

local Player = game.Players.LocalPlayer

script.Parent.Equipped:Connect(function()
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("RadioUI") then 
		game.Players.LocalPlayer.PlayerGui.RadioUI:Destroy()
	else
		warn("No GUI to Destroy.")
	end
	
	game.ReplicatedStorage.StopSoundEvent:FireServer(Player)
	
	local RadioUI = game.Lighting.RadioUI:Clone()
	RadioUI.Parent = game.Players.LocalPlayer.PlayerGui
	
	RadioUI.Frame.TB1.PlayMuteButton.MouseButton1Click:Connect(function()
		local ID = RadioUI.Frame.TL1.ID.Text
		game.ReplicatedStorage.PlaySoundEvent:FireServer(ID)
	end)
end)

script.Parent.Unequipped:Connect(function()
	if game.Players.LocalPlayer.PlayerGui:FindFirstChild("RadioUI") then 
		game.Players.LocalPlayer.PlayerGui.RadioUI:Destroy()
	else
		warn("No GUI to Destroy.")
	end
	
	local Music = script.Parent.Handle.Music
	game.ReplicatedStorage.StopSoundEvent:FireServer(Music)
end)

I will test it out with the prints.

Full server script:

game.ReplicatedStorage.PlaySoundEvent.OnServerEvent:Connect(function(Player, ID)
	if tonumber(ID) then 
		Player.Character.BoomBox.Handle.Music.SoundId = "rbxassetid://" .. ID
		Player.Character.BoomBox.Handle.Music:Play()
	elseif tostring(ID) then
		return
	end
end)

game.ReplicatedStorage.StopSoundEvent.OnServerEvent:Connect(function(Player, Music)
	Music:Stop()
end)
1 Like

Ok, so I tested it and this is what happened:

https://gyazo.com/0e0f4c2ae49d0e040a99fb6f97c2cb2c

1 Like

I don’t know but I’m just gonna take a guess and say it has to do with the music play duration after the boombox is unequipped.

1 Like

Alright. From what I can see, the error you’re getting is probably because the script is looking for the Music object in the player, and it thinks Music is a child of Players.dmksa123 when in fact it is a child of Players.dmksa123.Backpack.Boombox.Handle

1 Like

Yeah, seems like that is happening, is there a solution to fix that, or will I just have to forget about the error?

The error color is green, so the error is in the server script, I think

1 Like

However, I don’t see Stop written in the script anywhere

1 Like

Alright, so in the localscript where it says print(script.Parent.Name) could you do script.Parent.Parent.Name instead? I forgot to put another .Parent in there and it printed the tool name instead of the parent of the tool.

1 Like

Yeah, I will try to print that out.

Ok, this is what it prints out.

Hm. That’s odd. Does the script still work even with the error?

1 Like

Yeah, should I just pcall the line of code?

Sure. I mean, I’m not even sure why the error is there.

1 Like

Ok, thank you for trying to help me figure out the error.

When it gives you the error, click on the error and see what line it directs you to.

Line 14, however now since I put a pcall on the error, the error stopped, so Idek.