You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I want to make the radio music stop as soon as the radio was turned off
What is the issue? I can’t stop the music, and the “Started” boolValue doesn’t change despite the script being able to go through it.
What solutions have you tried so far? I have tried putting the sound into Debris Service, tried using :Stop(), .Playing = false, :Destroy(), :Remove(), etc.
local Title = script.Parent.SurfaceGui.Title
local Radio = script.Parent
local Prompt = Radio.ProximityPrompt
local Started = Radio.Parent.Started.Value
local MusicFolder = Radio.Parent.MusicFolder:GetChildren()
local Musics = Radio.Parent.MusicFolder
local Debris = game:GetService("Debris")
Prompt.Triggered:Connect(function()
local Music = MusicFolder[math.random(1, #MusicFolder)]:Clone()
if Started == false then
Started = true
Prompt.ActionText = "Turn off"
Radio.Start:Play()
Radio.Static:Play()
Title.Text = "..."
wait(1)
Music.Parent = Radio
Music:Play()
Title.Text = Music.Name
else
Started = false
Prompt.ActionText = "Turn on"
Title.Text = " "
Radio.Start:Play()
Radio.Static:Stop()
Music:Destroy()
print("Stopped")
end
end)
The code above is the one that I used, it’s in a server script, located inside the radio part.
The musics are not looped, the static sound is the only one that’s looped.
The script did go through the print(“stopped”) part.
Change line 4 to: local Started = Radio.Parent.Started
and use Started.Value = true to set the value to true
When you set local Started = Radio.Parent.Started.Value, the variable just takes the value of ‘Started’, in this case, it would be a normal boolean variable (false).
I’ve changed it, still didn’t work. The Started value did change tho.
local Title = script.Parent.SurfaceGui.Title
local Radio = script.Parent
local Prompt = Radio.ProximityPrompt
local Started = Radio.Parent.Started
local MusicFolder = Radio.Parent.MusicFolder:GetChildren()
local Musics = Radio.Parent.MusicFolder
local Debris = game:GetService("Debris")
Prompt.Triggered:Connect(function()
local Music = MusicFolder[math.random(1, #MusicFolder)]:Clone()
if Started.Value == false then
Started.Value = true
Prompt.ActionText = "Turn off"
Radio.Start:Play()
Radio.Static:Play()
Title.Text = "..."
wait(1)
Music.Parent = Radio
Music:Play()
Title.Text = Music.Name
else
Started.Value = false
Prompt.ActionText = "Turn on"
Title.Text = " "
Radio.Start:Play()
Radio.Static:Stop()
Music:Destroy()
print("Stopped")
end
end)
I found the solution, I don’t know why it works this time but it did.
Here’s the new code:
local Title = script.Parent.SurfaceGui.Title
local Radio = script.Parent
local Prompt = Radio.ProximityPrompt
local Started = Radio.Parent.Started
local MusicFolder = Radio.Parent.MusicFolder:GetChildren()
local Musics = Radio.Parent.MusicFolder
local Debris = game:GetService("Debris")
local function ChooseMusic(Music)
if Started.Value == true then
Radio.Static:Play()
Music.Parent = Radio
Title.Text = "..."
Prompt.ActionText = "Turn off"
wait(1)
Music:Play()
Title.Text = Music.Name
end
end
local function StopMusic(Music)
if Started.Value == false then
Prompt.ActionText = "Turn on"
Title.Text = " "
Radio.Static:Stop()
Music:Destroy()
end
end
Prompt.Triggered:Connect(function()
local Music = MusicFolder[math.random(1, #MusicFolder)]:Clone()
Radio.Start:Play()
if Started.Value == false then
Started.Value = true
ChooseMusic(Music)
else
Started.Value = false
end
Started.Changed:Connect(function()
StopMusic(Music)
end)
Music.Ended:Connect(function()
ChooseMusic(Music)
end)
end)
Thanks to those who have helped me with this problem.