This is a very simple script, yet it seems to have a glitch.
local cue=script.Parent.Music:GetChildren() local r=math.random(1,#cue) local song=cue[r]
It returns no errors, but it always returns 1, which is the very first song. Then it plays the second, then the third, and so on. I want it to choose randomly, but the math.random function always returns 1.
Well, math.random will ALWAYS return the same numbers. For example, if you ran:
for _ = 1, 10 do print(math.random(100)) wait(1) end
It would print the same 10 numbers every time. To fix this, you need to use math.randomseed(tick())
. That would return different numbers every time the place ran.
Read more on the wiki here.
local songs = {} -- empty table so we can insert the names later local song -- call the variable song as nil just for cus :P for _, v in pairs(script.Parent.Music:GetChildren()) do -- loop through the Music thingy table.insert(songs, v.Name); -- inserts each child's name in the songs table for later usage end song = tostring(songs[math.random(1, #songs)]); -- now we us the variable song and tell have it be a random song!