How would I check if the second song played isn't the same as the other in a random playlist?

Hi there, I have a music playlist script which goes through a table and randomly plays a song one at a time. The issue is, is that when the a song ends and goes through the next song to play, there’s a chance that the math.random could go to the same value and play that same song again. I don’t have an attempt as I am unsure on what to do.

local MPS = game:GetService("MarketplaceService")
local currentTrack = game.ReplicatedStorage.CurrentTrack 
local CurrentCamera = workspace.CurrentCamera
local musicplayer = workspace.Sound
local id = game.ReplicatedStorage.songid

music = {
	"rbxassetid://3044286747",
	"rbxassetid://1256391355",
	"rbxassetid://5480102561",
	"rbxassetid://5024527209",
	"rbxassetid://870242868",
	"rbxassetid://1112109949",
	"rbxassetid://6303331764",
	"rbxassetid://2206587324",
	"rbxassetid://5987864893",
	"rbxassetid://534429024",
	"rbxassetid://4551177864",
	"rbxassetid://664277618",
	"rbxassetid://3011342543",
	"rbxassetid://3523361267",
	"rbxassetid://3616424949";

}

while true do 

	if not musicplayer.IsPlaying then

		local RandomSong = music[math.random(1, #music)]
		local SongID = string.match(RandomSong, "%d+")
		local SongInfo = MPS:GetProductInfo(SongID)

		musicplayer.SoundId = RandomSong
		musicplayer:Play()

		currentTrack.Value = "..." -- displays the song playing in a music player gui

		currentTrack.Value = SongInfo.Name -- just for displaying current song playing

		musicplayer.Ended:Wait()

		game:GetService("RunService"):UnbindFromRenderStep("CurrentTrack")

	end
end
1 Like

One way to do this is to make a reference to your chosen song. Repeat math.random until the ‘random song’ isn’t the ‘chosen song’, then replace ‘chosen song’ with ‘random song’.

How would I do this? My head is getting mixed around, sorry.

local OldSong
local RandomSong
repeat RandomSong = music[math.random(1, #music)] until RandomSong ~= OldSong -- Randomise until the chosen song isn't the old song.
OldSong = RandomSong -- Replace Old Song with the new song

It is confusing lol but here.

Ohh, I understand now. I’ll try this out.