How can I make my radio skip the music?

Hello programmers, I’m not good with scripting by any means, but I am trying to make a radio in a game for my friends and I to enjoy.

local Sound1 = game.Workspace.Mechanic.Props.Radio.Playlist.Sound1
local Sound2 = game.Workspace.Mechanic.Props.Radio.Playlist.Sound2
local Sound3 = game.Workspace.Mechanic.Props.Radio.Playlist.Sound3
local Sound4 = game.Workspace.Mechanic.Props.Radio.Playlist.Sound4
local Detector = game.Workspace.Mechanic.Props.Radio.ClickDetector
---------------------------------------------
while true do
	Sound1:Play()
	Sound1.Ended:Wait()
	wait(1)
	Sound2:Play()
	Sound2.Ended:Wait()
	wait(1)
	Sound3:Play()
	Sound3.Ended:Wait()
	wait(1)
	Sound4:Play()
	Sound4.Ended:Wait()
	wait(1)
end

This is what I currently have to play songs from the playlist. What I want to achieve is to be able to skip the current playing music to the next one by clicking on the mesh. How can I accomplish this? Thank you.

2 Likes

You’ll probably need to use a for loop, check which number the sound is, and when the ClickDetector is activated jump to the next sound

2 Likes

So when you have set up the way you want the music to be skipped you’ll simply take the currently playing music and set the sound’s TimePosition to the TimeLength. TimePosition determines where the song is currently at the TimeLength is the length of the audio playing.

1 Like

I couldn’t figure this out and get the code to work, can you show an example? Thank you.

1 Like

Well, I’m not an expert but you would do something like:

local songsTable = {
sound1 = theOathToTheSound -- do this for etch sound you have
}

local detector = --too lazy to write everything

for _, songsTable in pairs(songsTable) do
   local currentlyPlaying = songsTable
   currentlyPlaying:Play()
   currentlyPlaying.Ended:Wait()
   wait(1)
   return currentlyPlaying
end --SHOULD play all the songs by looping, and returns currentlyPlaying 

local function onClick()
   currentlyPlaying.TimePosition = currentlyPlaying.TimeLenght
end

detector.Triggered:Connect(onClick)

This should work, I’m not sure as I didn’t test it

1 Like
local function onClick()
	CurrentlyPlaying.TimePosition = CurrentlyPlaying.TimeLenght
end

Detector.Triggered:Connect(onClick)

In this part it couldn’t recognize “CurrentlyPlaying” giving the error; “Unkown global ‘CurrentlyPlaying’”.

Then add CurrentlyPlaying as a nil variable at the start, like:

local CurrentlyPlaying

At the start

You probably meant to not return in a for loop?

1 Like

Why? Shouldn’t it return the song?
Sorry I’m not an expert in return, it’s the 2nd time I use it

1 Like
for _, songsTable in pairs(songsTable) do
   local currentlyPlaying = songsTable
   currentlyPlaying:Play()
   currentlyPlaying.Ended:Wait()
   wait(1)
   return currentlyPlaying --< The entire script will stop here
end

-- Code never reached:
local function onClick()
   currentlyPlaying.TimePosition = currentlyPlaying.TimeLenght
end

detector.Triggered:Connect(onClick)

The script is returning a value to nothing, and the script stops when in the for loop.

1 Like

Something like this should work:

local playlist = workspace.Mechanic.Props.Radio.Playlist
local songs = {}
local last -- Last song played

for _, obj in pairs(playlist:GetChildren()) do
   if obj:IsA("Sound") then
      table.insert(songs, obj)
      obj.Looped = false
   end
end

local function SkipSong()
   for _, song in pairs(songs) do
      song:Stop()
   end
end

task.spawn(function()
   while true do
      local song
      repeat
         song = songs[math.random(1, #songs)]
         if song == last then song = nil end -- Don't pick same song twice
      until song
      last = song
   
      warn(song, "is now playing!")
      song:Play()
      task.wait(1)
   
      if song.IsLoaded then
         song.Ended:Wait()
         task.wait(1)
      end
   end
end)
2 Likes

Okay, so the second should be wrapped in a coroutine?

This works but doesn’t end the sound that is currently playing.

What do you mean? It doesn’t end when you call SkipSong()?

I couldn’t understand the problem, looks like it plays a random sound from the playlist but doesn’t skip when I click the radio. Nothing in the output or no errors.

It isn’t coded to actually call SkipSong(), you’d have to add that.