Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mute Radios button doesn't work when a player resets?

Asked by 3 years ago

Hello!

I have a radio gampeass in my game which grants access to a Radio ingame. The radio works it self, however I am trying to make a mute radio button for people to mute music such as bypasses. This does work, however, the issue with the script is that when a Player with the radio playing a sound resets / rejoins, their audio can then be heard again by players. I'm not exactly sure what is causing this. Here is the script I am using:

--###----------[[SERVICES]]----------###--
local Players = game:GetService("Players")




--###----------[[VARIABLES]]----------###--

-- Objects
local MuteButton = script.Parent


-- Regular
local MuteToggle = true




--###----------[[FUNCTIONS]]----------###--

-- Sound Management
local function ManageRadioMute()
    for _,Player in pairs(Players:GetPlayers()) do
        local Character = Player.Character
        if (Character) then
            ---------------
            local Radio = Character:FindFirstChild("Radio")
            if (Radio) then
                ---------------
                local Sound = Radio:FindFirstChildWhichIsA("Sound")
                if (Sound) then
                    Sound.Volume = (MuteToggle and 0 or 1)
                end
            end
        end
    end
    MuteToggle = (not MuteToggle)
end



--###----------[[SETUP]]----------###--
MuteButton.MouseButton1Click:Connect(ManageRadioMute)

3 answers

Log in to vote
0
Answered by 3 years ago

Hey, does the GUI have ResetOnSpawn enabled? If so, that could be the issue. Let me know if this fixes it.

0
Hello, thank you for the answer. The issue isn't that the GUI doesn't respawn with the character, the issue happens with the script. Upon a player muting all radios, any player with a radio could just reset, and play an audio and everybody who muted the radios prior to the person resetting, would be able to hear their audio. I think the issue is caused that the radio resets it's volume on respawn. RazzyPlayz 497 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

in theory, you could have it manually replicate (RemoteEvents) the volume to every player, and the client script will decide whether or not to actually apply it or ignore the volume (based on the player's preferences). for this i would recommend having the actual volume of the Sound object to 0 to prevent unwanted issues, when a player wants to play music, send a request via RemoteEvent to the server to play to all users, each player will receive the OnServerEvent and the LocalScript will decide based on the player's preferences to set the volume as 1 or 0.

a code example: in server script

local RemoteEvent = game:GetService('ReplicatedStorage').SoundEvent; -- this is the remoteevent to use

RemoteEvent.OnClientEvent:Connect(function(player, sound)
    RemoteEvent:FireAllClients(sound);
end);

in client script

local muted = false;

local RemoteEvent = game:GetService('ReplicatedStorage').SoundEvent; -- this is the remoteevent to use

RemoteEvent.OnServerEvent:Connect(function(sound)
    -- i would recommend having an id blacklist here instead of the server
    if (not muted) then
        sound.Volume = 1;
    end
end);

you can use your posted code to mute all radios whenever it is pressed, this is just to make sure it stays muted

0
also for "sound" make sure to use the Sound object instead of the ID. kaytikookie 45 — 3y
0
Thank you for the answer however the radio does not replicate it's sound to everybody, it's just a playing sound in a model behind the back. RazzyPlayz 497 — 3y
Log in to vote
0
Answered by
I_Nev 200 Moderation Voter
3 years ago

You could use a character added event, since you said it is caused when they reset, you can make it so either when the player is added redo the script or have a event in the music player that fires to all clients when the id changes, that way you can check it per client

0
The script checks for the character ingame for the radio, how would I go to use the function? RazzyPlayz 497 — 3y

Answer this question