Hello,
Recently, I’ve been thinking about making a system where a random music from a specific table is chosen, and the music starts playing. However, I can’t seem to find a way to choose a track randomly. Any suggestions?
Code:
Hello,
Recently, I’ve been thinking about making a system where a random music from a specific table is chosen, and the music starts playing. However, I can’t seem to find a way to choose a track randomly. Any suggestions?
Code:
If you do
Music[“Track”..tostring(math.random(1,#Music))]
it will return a random Track.
local function GetRandomTrack()
local Tracks = {}
for i, Track in pairs(Music) do
table.insert(Tracks, Track)
end
return Tracks[math.random(#Tracks)]
end
print(GetRandomTrack())
That won’t work because # only counts number indeces
That seems to print something like table: xce20392 and not the chosen track
That’s the table with the track info, try printing GetRandomTrack().Name
or GetRandomTrack().ID
Your code won’t work either. You are just copying each track from the Music table to the Tracks table and then returning the last one in the list with Tracks[#Tracks].
Also, you won’t get the index “i” from your for-loop unless you use ipairs on an array (list-like table). His music table is indexed by names.
Run it in studio and see if it works Lol
It did, I tried it on studio. I made the system I wanted using that and loops.
Using “pairs” as a pseudo-random generator is poor practice, in my opinion. He should organize the tracks into an array and use math.random(1, #array) as the index for track retreival.
Oh yea, mb, I forgot math.random Lol
You should first convert your Music dictionary of tracks into an array of tracks like so:
Music = {
{
Name = "...",
ID = 1234567890,
},
...
}
Then, you can index the Music table like so:
print(Music[1]) --> Gives the first track info.
print(Music[1].Name) --> Gives the name for the first track.
print(Music[1].ID) --> Gives the ID for the first track.
Now the problem is how to randomly select a indice. You can use math.random()
to do so. Give it the minimum range and the maximum range like so:
print(Music[math.random(1, #Music)].Name) --> A random track's name.
You can wrap it into its own function like so:
local function SelectRandomTrack()
return Music[math.random(1, #Tracks)]
end
Edit:
I almost forgot, you should actually use Random instead. It’s a better random number generator. You can click on the hyperlink on how it works.
This is the right answer.
EDIT: Can you elaborate on why Roblox’s Random datatype is better than math.random? Is it faster?
I suggest you use the method @utrain did. Also, I fixed my code if you still wanna use that one for whatever reason
It isn’t because it is faster. It is because it is a better RNG generator. It produces a stream of numbers closer to random than math.random(). Additionally, it is more reproducible since there isn’t multiple sources invoking the same generator. So, if you have a gun and dedicate one Random object to it for recoil, you can consisently reproduce the same results if you manually set the seed. You know for sure that nothing else is effecting the results.