I’m trying to make a radio part which plays continuously in the server to anyone who is closer to it can hear and which any player can mute it locally, this may avoid people listening different songs.
The issue right there is for some reason the songs are not changing, perhaps there is a error in the function, index is not adding?
I tried recalling the function to restart the playlist, made some prints and everything looks normal, i used roblox AI to help but didn’t work well. The IDs i’m using are also ok.
-- Script for a radio with a playlist of 3 sounds and a click detector
local radio = script.Parent
-- List of sound ids for the playlist
local playlist = {
"rbxassetid://id0",
"rbxassetid://id1",
"rbxassetid://id2"
}
-- Index to keep track of the current sound in the playlist
local currentIndex = 1
-- Function to play the current sound
local function playSound()
local soundId = playlist[currentIndex]
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Parent = radio
sound:Play()
end
print("play sound")
wait(1)
-- Function to stop the current sound
local function stopSound()
local sound = radio:FindFirstChildOfClass("Sound")
if sound then
sound:Stop()
sound:Destroy()
end
end
print("destroy")
wait(1)
-- Function to handle the click event
local function onClick()
-- Toggle mute state
local sound = radio:FindFirstChildOfClass("Sound")
if sound then
sound.Volume = sound.Volume == 0 and 1 or 0
end
end
-- Function to handle the end of a sound
local function onSoundEnded()
-- Stop the current sound
stopSound()
-- Move to the next sound in the playlist
currentIndex = currentIndex + 1
if currentIndex > #playlist then
currentIndex = 1
end
-- Play the next sound
playSound()
end
print("sound ended")
wait(1)
-- Connect the click event to the onClick function
radio.ClickDetector.MouseClick:Connect(onClick)
-- Connect the end event of the current sound to the onSoundEnded function
local sound = radio:FindFirstChildOfClass("Sound")
if sound then
sound.Ended:Connect(onSoundEnded)
end
-- Play the first sound
playSound()
If anyone can help telling me which is the issue here, only the number “1” song is playing. Couldn’t find anyone talking about this similar occasion. I’m a builder not much experienced to programming Lua scripts.
What is happening: (Starting test) > sound “1” plays > stops and is not going to the next one [also Sound created keep exists in workspace and is not destroyed as it should].
Putting playSound() after the event connection will cause sound to be nil when it is first searched for in the line local sound = radio:FindFirstChildOfClass("Sound").
Furthermore, your code also has a few other errors that are hindering your debugging:
The line print("sound ended") is outside the function, which means it will always print when the script is first ran, regardless of whether the function is actually called or not. That’s why the times in your output are so close together (a few seconds)
Also, please do not use wait(). Instead of wait(1), do task.wait(1) as it is more efficient.
Ok I had to recreate the script with the roblox IA and it worked pretty fine for now after some time, if anyone wishes to see the full script here is, you can edit settings and add the IDs in the table of your own.
-- Script for a radio that plays a list of songs
local part = script.Parent
-- List of song IDs
local songIDs = {
0,
0,
0,
0,
}
-- Configuration settings
local volume = 0.7
local pitch = 1
local playbackSpeed = 1
local rollOffMaxDistance = 250
local rollOffMinDistance = 100
local rollOffMode = Enum.RollOffMode.InverseTapered
-- Function to play a song
local function playSong(songID)
-- Create a Sound object
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://" .. songID
sound.Volume = volume
sound.Pitch = pitch
sound.PlaybackSpeed = playbackSpeed
sound.RollOffMaxDistance = rollOffMaxDistance
sound.RollOffMinDistance = rollOffMinDistance
sound.RollOffMode = rollOffMode
sound.Parent = part
wait(5)
-- Play the song
sound:Play()
-- Create a ReverbSoundEffect inside Sound object
local reverb = Instance.new("ReverbSoundEffect")
reverb.Parent = sound
-- Properties of the ReverbSoundEffect
reverb.Enabled = true
reverb.DecayTime = 1
reverb.Density = 0.7
reverb.Diffusion = 0.8
reverb.DryLevel = -6
reverb.WetLevel = 0.4
-- Wait for the song to finish
sound.Ended:Wait(1)
-- Clean up the Sound object
sound:Destroy()
end
-- Function to shuffle the song list
local function shuffleList(list)
local random = Random.new()
for i = #list, 2, -1 do
local j = random:NextInteger(1, i)
list[i], list[j] = list[j], list[i]
end
return list
end
-- Shuffle the song list
songIDs = shuffleList(songIDs)
-- Variable to track the current volume state
local isMuted = false
-- Function to handle the click event
local function onClick()
-- Toggle mute state
local sound = part:FindFirstChildOfClass("Sound")
if sound then
sound.Volume = sound.Volume == 0 and volume or 0
end
end
part.ClickDetector.MouseClick:Connect(onClick)
-- Loop through the song list
while true do
for i, songID in ipairs(songIDs) do
print("Playing song: " .. songID)
playSong(songID)
end
-- Reshuffle the song list after playing all songs
songIDs = shuffleList(songIDs)
end