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.
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.
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
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.
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)
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.