How to make a random song player?

I’m trying to make a script that plays a random selection of songs.
Does math.random produce a new number every time it’s called upon?
like

local song = math.random(1,2)

local s1 = script.sound1
local s2 = script.sound2

while true do
      if song = 1 do
            s1:play
      end
      else if song = 2 do
            s2:play
      end
end

and then like

if s1.playing == false or s2.playing == false
end?

The last part would check if the song is still playing and if not it would repeat and play another song

1 Like

math.random will produce a new number if you put it in repeatable loop, in your script the math.random will be set just once.

1 Like

Math.random() will get a random number But in a loop it will only choose once Try this code Instead.



local s1 = script.sound1
local s2 = script.sound2

while task.wait(3) do -- change the 3 to what number you would like it to change the song.
local song = math.random(1,2)
      if song = 1 do
            s1:Play()
           s1.Ended:Wait()
      end
      else if song = 2 do
            s2:Play()
            s2.Ended:Wait()
      end
end
1 Like

This script will constantly play the audios whether they’re finished or not, you’d want to do this

local sound1 = script.sound1
local sound2 = script.sound2

while true do
	task.wait(0.3)
	local counter = math.random(1,2)
	
	if counter == 1 then
		sound1:Play()
		sound1.Ended:Wait()
	elseif counter == 2 then
		sound2:Play()
		sound2.Ended:Wait()
	end
end
4 Likes