I have it figured out how I can get a random song from a folder that I have with them in
stationChoice[math.random(1,#stationChoice)]
but how would I get the next one in the list instead of getting a random once each time?
I have it figured out how I can get a random song from a folder that I have with them in
stationChoice[math.random(1,#stationChoice)]
but how would I get the next one in the list instead of getting a random once each time?
Hey.
Depends if you already have the station initialized if yes, then you would just do this
local station = --already initialized station
local nextStation = stationChoice[table.find(stationChoice, station) + 1]
If not then you will have to create some kind of other system.
Also make sure the station is inside the stationChoice
table and that the nextStation
isn’t nil since the next station might be nil because you will run out of bounds.
You can do something like this:
local currentStation = 0
local function nextStation()
currentStation += 1
if currentStation > #stationChoice then
currentStation = 1
end
return stationChoice[currentStation]
end
just do something like this if you want to get the (next) sound:
local sound = nextStation()
What would you mean by already initialized
Just add one to your first index.
local firstIndex = math.random(#stationChoice)
local firstSong = stationChoice[firstIndex]
local secondIndex = firstIndex + 1
local secondSong = stationChoice[secondIndex]
And so on for the rest. Reuse the same variable so you don’t have to literally write out all the possible indexes.
Eventually you’ll fall off the end of the list, so you’ll need to handle that too.
Get the playing song and play the next one in the table. Add 1 to your index as the reply above me said.
local station = stationChoice[1]