Hello! I am trying to make a “Mute Radio” button, and I have a script, but when I press the button it says "attempt to index nil with ‘Volume’. Here is my mute script:
local on = true
script.Parent.MouseButton1Click:Connect(function()
if on == true then
game:GetService("SoundService"):FindFirstChild("Radios").Volume = 0
script.Parent.Text = "Unmute Radios"
on = false
else
on = true
game:GetService("SoundService"):FindFirstChild("Radios").Volume = 0
script.Parent.Text = "Mute Radios"
end
end)
Here is the server side of the radio code:
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Remote = Tool:WaitForChild("Remote")
local Sound = Tool.Handle:FindFirstChild("Sound")
local LastRequestTime = 0
function floodCheck()
local timeNow = tick()
local elapsed = timeNow - LastRequestTime
if elapsed > 0.5 then
LastRequestTime = timeNow
return false
end
return true
end
function onUnequip()
Sound:Stop()
end
function onActivate()
Remote:FireClient(getPlayer(), "ChooseSong")
end
function getPlayer()
return game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
end
function isPotentiallyValid(id)
local idNumber = tonumber(id)
if not idNumber then
return false
end
if idNumber > 100000000000000000000 then
return false
end
return true
end
function playSong(id)
id = id or ""
if not isPotentiallyValid(id) then
return
end
if not Sound then
Sound = Instance.new'Sound'
Sound.SoundGroup = game:GetService("SoundService"):FindFirstChild("Radios")
end
Sound.Parent = Handle
Sound.Volume = 1
Sound.Looped = true
Sound.PlayOnRemove = false
Sound.SoundId = "http://www.roblox.com/asset/?id="..id
Sound:Play()
end
function onRemote(player, func, ...)
if floodCheck() then return end
if player ~= getPlayer() then return end
if func == "Activate" then
onActivate(...)
end
if func == "PlaySong" then
playSong(...)
end
end
Remote.OnServerEvent:connect(onRemote)
Tool.Unequipped:connect(onUnequip)
Thanks for any help!