Math.random playlist script

So I’ve successfully made a Playlist who plays random music. But every time a song ends, the same one plays again.
How can i keep the math.random number change every time a song ends?
Here is the script i used:

local sound = game.Workspace.Sound
local Mark = 4933853246
local LeatherFace = 4933839370
local Savanah = 1316845233



 while true do
	local random = math.random(1,3)

if random == 1 then
sound.SoundId = "rbxassetid://"..Mark
sound:Play()
wait(sound.TimeLength)
end

if random == 2 then
 sound.SoundId = "rbxassetid://"..LeatherFace
    sound:Play()
wait(sound.TimeLength)
end

if random == 3 then
 sound.SoundId = "rbxassetid://"..Savanah
    sound:Play()
wait(sound.TimeLength)
end
end

Thank you for helping :heart:

6 Likes

Its should already change, because math.random is called in eatch loop.
To check that you can print(random)

So your script should work.

1 Like

I think Your script Should Work.

Keep track of the previous number and if the selected random number is the same the previous one, repeat the random calls until it’s different using repeat until loop.

1 Like

Adding to that you can make a check so it doesn’t play the same music if the math.random choose it again.

While your script does look functional at a glance, my guess is you were running it once and seeing the same result every time. Computers have a hard time being random, here’s an interesting paper on the subject if you want to know more. Your seed each time is most likely the same, this can be simply fixed by adding math.randomseed(os.time()) to the beginning of your script, which will set your seed to the current UTC timestamp. Also, as others have already mentioned, you might want to add a check to see if the current roll is the same as the last one.

3 Likes

You could put the ID’s in a table maybe. You could find someway to make that work.
The code is not the best but it’ll work.

2 Likes

First, I personally recommend using a table to store the sound IDs

sounds = {4933853246, 4933839370, 1316845233}

Next, you’d have to pick a sound from the table.

local soundToPlay = sounds[math.random(1, #sounds)]

Finally, just make a while true loop containing this code:

while true do
    local soundToPlay = sounds[math.random(1, #sounds)]

    sound.SoundId = 'rbxassetid://' .. tostring(soundToPlay)
    sound:Play()
    wait(sound.TimeLength)
end

This is my first time posting on the forums, so I’m sorry if this is messy, doesn’t work, etc!

10 Likes

@Trystanus i waited 4 songs thinking that it might be broken and it played the exact same song 4 times in a row

@BabyDevil285 Thank you so much for helping. One problem i noticed tho

sound.SoundId = 'rbxassetid:// .. tostring(soundToPlay)'

should be

sound.SoundId = 'rbxassetid://'..tostring(soundToPlay)

@Chemiclast Thank you for that info, i’ll check it as soon as possible :heart:

1 Like

Here’s an untested example code for you that I just wrote. The code works by setting randomly picking a sound until the selected sound isn’t the same as the previous sound. After it finds a different sound, it’ll set the previous sound to the selected sound and do the rest of the logic for you. Additionally, it might be a good idea to use a table for your sounds, so I added that for you as well.

local random = Random.new()

local sounds = {4933853246, 4933839370, 1316845233}

local previousSound

local sound = path.to.sound

while true do
	local selectedSound
	
	repeat
		selectedSound = sounds[random:NextInteger(1, #sounds)]
	until selectedSound ~= previousSound
	
	previousSound = selectedSound	
	
	sound.SoundId = "rbxassetid://" .. selectedSound
    sound:Play()
    wait(sound.TimeLength)
end

The above code is easy to follow and it should be clear. Let me know if you have any questions though.

1 Like

Quickly just wrote this too, similar to @mircostaff however it uses the Ended event of sound rather than a while loop which passes in the sound id that just finished.

math.randomseed(tick())
local SoundIds = {"4933853246", "4933839370", "1316845233"}

local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://" .. SoundIds[math.random(1, #SoundIds)]

Sound.Ended:Connect(function(SoundId)
    local newSoundId
	
    do repeat
        newSoundId = "rbxassetid://" .. SoundIds[math.random(1, #SoundIds)]
        wait() 
    until
        newSoundId ~= SoundId
    end
	
    Sound.SoundId  = newSoundId
    Sound:Play()
end)

Sound.Parent = workspace
Sound:Play()
2 Likes

The main reason why I didn’t use the Ended event over a while loop is due to the fact that I’m not entirely sure if there’s anything else occurring in the loop or not. If there’s nothing else, then the event is definitely the way to go but I just didn’t want to assume anything. :slight_smile:

2 Likes

One thing that might be causing it is that “random” in lua is a math term, as seen by the fact that it is a Navy Blue. Try changing it to randomSong. Also, it would be a much better idea to either:

  1. Have a table of sounds that BabyDevil Mentioned
    or
  2. Already have every song as a child of your “playlist maker” (i.e. radio or maybe even SoundService), name them a single number, in order, and then call it depending on what math.random is.
1 Like

There isn’t a pre-defined function called “randomSong” - that function would have to be created using a mathematic random algorithm.

1 Like

I added more songs to the script you’ve showed me and everything works until a song stops. A random song might start but in most of the cases, it stays silent for a whole song time or the songs start from a random track point. I checked all the ID’s and they are correctly written. Nothing else is changed at the script

2 Likes

Strange, sorry about that. Didn’t notice it happening when I tested it.

I’ve updated the Ended event to include stopping the sound and resetting the TimePosition to 0, and it seemed to fix this from happening.

Sound.Ended:Connect(function(SoundId)
    local newSoundId
	
    do repeat
        newSoundId = "rbxassetid://" .. SoundIds[math.random(1, #SoundIds)]
        wait() 
    until
        newSoundId ~= SoundId
    end
	
	Sound:Stop()
    Sound.SoundId  = newSoundId
	Sound.TimePosition = 0
	
    Sound:Play()
end)
7 Likes

Define the randomSong as math.random(1,3)

Taking a quick look at the code will show that it detects it as a mathematical statement, seeing at how it is navy blue.

Defining math.random(1, 3) as randomSong won’t do much besides set the randomSong variable to the number generated by the random function. The random function has to be called each time a new random song needs to be selected.

The color does not mean it is a mathematical statement, although math.random is, the color is due to syntax highlighting of built-in functions.

error, pcall, print, math.random

It becomes useful when you change the names of the actual sounds themsevles:

  1. Place each song you wish to play in SoundService. Name each song “1”, then “2”, then “3”, so on so forth.
    Now, if this is a game-wide thing then put a script in ServerScriptService, else in the block you wish for the sound to be emitted from. This is how it would go:
local function randomSound()
   local v = math.random(1,3)
   game.SoundService[v]:Play()
   game.SoundService[v].IsPlaying.Changed:Connect(function()
  if game.SoundService[v].Playing == false then
  randomSound()
end
end)
end)

edit: I used .Playing instead of :Play(), so I fixed that.