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
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.
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.
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!
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.
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()
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.
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:
Have a table of sounds that BabyDevil Mentioned
or
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.
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
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.
It becomes useful when you change the names of the actual sounds themsevles:
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.