Random Music Playlist Bug

So i have this bug on my random playlist script. The bug is even though i set this to loop through every singlething on the table randombly 1 by one its still sometime play the same song after one song is finished i wonder what’s wrong

local ids = {6045803313,6045799419,5977968880,288756677,2547086627,743287013,665711112,3044286747,269096289,603146550,639750143,170902050,188225491,144183348,230210363,4758766451,468891571,918941572,714630890,265512831,259816079,311316650,} -- your song ids here
repeat wait()
	for i = 1, #ids, 1 do -- loops through the table
		local songObj = Instance.new("Sound")
		local random = Random.new()
		songObj.Parent = game:GetService("SoundService")
		local nowPlaying = ids[random:NextInteger(i,#ids)]
		print("playing song No. " .. i)
		songObj.SoundId = "rbxassetid://"..nowPlaying
		songObj.Volume = 1
		songObj:Play()
		songObj.Ended:Wait()
		songObj:Destroy()
	end
until false

Well, you are reseeding your random value every time you pick a new song. Which already decreases the randomness of your song selection. Declare Random.new() outside the loop.

2 Likes

but the

local nowPlaying = ids[random:NextInteger(i,#ids)]

number of i increases so that it will pick a random song within that range i wonder why it choses that same song again can you explain?

Well, because you designed your code to allow it for to do that…

Reread your code and try and understand it. Right now your code will give any Id value with a larger index in the table a higher chance of playing as time goes on. This is because you are decreasing the range of the random value. Technically the last song in the list can play every time if it is lucky enough.

2 Likes

is there a way to remove or disable played ids in the table and revert it back when the table becomes empty?

is there also a way to seth the position of the items in the table randomly so that i’ll just loop through that randomly arranged table

Use two tables.

Table A is the reference table which will be used to store all the Ids which Table B will use to copy.

Table B is a copy of Table A. When a song is played removed said SoundId from Table B. When Table B is empty, set Table B to a clone of Table A.

This would make it so songs could still be played in a random order, but all songs must be played once before a song can repeat.

1 Like
local OrigIds = {6045803313,6045799419,5977968880,288756677,2547086627,743287013,665711112,3044286747,269096289,603146550,639750143,170902050,188225491,144183348,230210363,4758766451,468891571,918941572,714630890,265512831,259816079,311316650} -- your song ids here
local ids = {6045803313,6045799419,5977968880,288756677,2547086627,743287013,665711112,3044286747,269096289,603146550,639750143,170902050,188225491,144183348,230210363,4758766451,468891571,918941572,714630890,265512831,259816079,311316650,} 
local random = Random.new()
repeat wait()
	for i = 1, #ids, 1 do -- loops through the table
		local songObj = Instance.new("Sound")
		songObj.Parent = game:GetService("SoundService")
		local nowPlaying = ids[random:NextInteger(1,#ids)]
		table.remove(ids[nowPlaying)
		print("playing song No. " .. i)
		print("no of ids left " .. #ids)
		songObj.SoundId = "rbxassetid://"..nowPlaying
		songObj.Volume = 1
		songObj:Play()
		songObj.Ended:Wait()
		songObj:Destroy()
		if #ids == 0 then
			ids = OrigIds
		end
	end
until false

will this work?

Have you tested the code in Studio? That should decide if it will work or not.

1 Like
local OrigIds = {6045803313,6045799419,5977968880,288756677,2547086627,743287013,665711112,3044286747,269096289,603146550,639750143,170902050,188225491,144183348,230210363,4758766451,468891571,918941572,714630890,265512831,259816079,311316650} -- your song ids here
local ids = {6045803313,6045799419,5977968880,288756677,2547086627,743287013,665711112,3044286747,269096289,603146550,639750143,170902050,188225491,144183348,230210363,4758766451,468891571,918941572,714630890,265512831,259816079,311316650,} 
local random = Random.new()
repeat wait()
	for i = 1, #ids, 1 do -- loops through the table
		local songObj = Instance.new("Sound")
		local randomNo = random:NextInteger(1,#ids)
		songObj.Parent = game:GetService("SoundService")
		local nowPlaying = ids[randomNo]
		table.remove(ids,randomNo)
		print("playing song No. " .. i)
		print("no of ids left " .. #ids)
		songObj.SoundId = "rbxassetid://"..nowPlaying
		songObj.Volume = 1
		songObj:Play()
		songObj.Ended:Wait()
		songObj:Destroy()
		if #ids == 0 then
			ids = OrigIds
		end
	end
until false

this version will work i think that one gives an error

thats not random players will get bored hearing that same sound pattern over and over again