Extremely simple boombox mute script that isn't working

Early disclaimer: I am obviously very new to scripting

I am using a boombox gear for my game. Here’s the link to the file to make it easier to describe: boombox mute.rbxm (7.7 KB)

It worked perfectly fine originally, but I am trying to add a feature to click to mute/unmute the audio playing on someone else’s boombox locally. I added in a ClickDetector in the boombox and added in a LocalScript I ripped from a free model (I know I know, please bear with me) and tried to modify it to change the volume of the sound when clicked from 0.5 to 0 and vice versa, and it isn’t working.

Here’s the script in case you don’t want to open the file:

local isOn = true

function on()
 isOn = true
 script.Parent.Sound.Volume = .5
end

function off()
 isOn = false
 script.Parent.Sound.Volume = 0
end

function onClicked()
 
 if isOn == true then off() else on() end

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

on()

Hmm, I would suggest using some prints to try and identify whether the functions on() and off() are actually being called at all. If they are, then it may be an issue with the script you grabbed from a free model that isn’t changing the volume correctly - if not try to identify whether the ClickDetector is functioning as it should do.

Just a quick question, is the boombox a model in the gamespace or is it a tool that the user would hold?

1 Like

It’s going to be tool players hold

Alright I see, well I believe the ClickDetector object works for players clicking objects in the game. If a player is holding the boombox, I suggest using the following function: Tool | Documentation - Roblox Creator Hub

This I think will do what you want it to do, just using a different method to call the functions in the first place.

If the boombox was on the floor for example and a player had to click on it in the gamespace, then yeah a ClickDetector would be necessary.

Hope this helps, -Tom :slight_smile:

1 Like

Click Detectors don’t work for tools. If you want a workaround, ScriptingHelpers made something a little while ago regarding this topic. How can I click a click detector with a tool equipped? - Scripting Helpers

2 Likes

Thanks for the reply, but I meant that players who have no tool equipped could click on other players’ boomboxes to mute and unmute them. Not to mute your own boombox.

I didn’t really explain it thoroughly enough, sorry!

What are you doing?

use a server script or if you want to do it for a client, locally, then use an event and bind it OnClientEvent in a local script in StarterCharacterScripts like :

Use a Server script to detect the click :

      local ReplicatedStorage = game:GetService("ReplicatedStorage")
      local event = ReplicatedStorage.RemoteEvent
      local players = game:GetService("Players")
      local player = players.LocalPlayer

script.Parent.ClickDetector.MouseClick:Connect(function()
    event:FireClient(player)--for this person
end)

In a local script in StarterCharacterScripts :
the sound you want to player should directly be parented to the player .

   local ReplicatedStorage = game:GetService("ReplicatedStorage")
   local event = ReplicatedStorage.RemoteEvent

   local players = game:GetService("Players")
   local player = players.LocalPlayer
   --maybe use a bool to check whether the sound is muted or something?

  local sound = player:WaitForChild("Sound")
  event.OnClientEvent:Connect(function()
	    if sound:IsPlaying() then 
	 	return	--prevent double-playing
      end
	 player.Sound:Play()
end)

Or to do it on the Server use a Script in the part.

   local part = script.Parent
   local sound = part.Sound
   part.ClickDetector.MouseClick:Connect(function()
	    if sound:IsPlaying() then 
		return
	end--if it is already playing then return , else play the sound
	sound:Play()
end)

To mute sounds for the player who clicked the mute button then check whether muted is true, using a boolean , if not then just Sound:Stop() it

2 Likes

I didn’t really care about an object’s ClickDetector working with a tool equipped, I needed a player to be able to click on another player’s boombox to mute it. So kind of the other way around.

Thanks for the reply though!

2 Likes