How to make sure that it doesn't play the same song again?

Hi i’d like to know how to make sure that when the scripts changes music, it doesn’t play the same one ?

script :

local sound = script.MusicPlayer
local songs = sound:GetChildren()
local nextSongPlayTime = 2
local songName = game.Workspace.Scripts.MusicScript.MusicPlayer:GetAttribute("SongName")
local index = math.random(1,#songs)

while true do
	sound.SoundId = "rbxassetid://"..songs[index].Value
	sound:SetAttribute("SongName", songs[index].Name)
	sound:Play()
	wait(sound.TimeLength)
	sound:Stop()
	wait(nextSongPlayTime)
end

Sometimes it plays the same one

1 Like

I would just loop through the songs with a for in pairs loop. This makes sure you get a random song and the already played songs don’t get played again until all songs were played.

Yes but I have a lot of songs ( around 50 ) and I dont want to write a big script just for that and it’s kind of not random

Your script won’t be bigger than your current and it is definetely random.

Wait I don’t really understand, could you write an quick example script because I don’t think I got your explanation :sweat_smile:

Use this:

while true do
    for i, v in pairs(songs) do

    end
end
1 Like

ok thanks imma try it !
charslimitoof

ok so i tried it but it doesn’t play the song randomly :sweat_smile:

maybe keep a variable called lastSong, pick a song at random, and if the current song is equal to the last song, then pick another one.

ok thanks imma try this ! Also thanks for Vaschex even though i’ll modify a bit your script

I think your solution may work but I have no idea how to script this :sweat_smile: i’m stuck when you have to run the else code:

local sound = script.MusicPlayer
local songs = sound:GetChildren()
local nextSongPlayTime = 2
local songName = game.Workspace.Scripts.MusicScript.MusicPlayer:GetAttribute("SongName")
local index = math.random(1,#songs)
local lastsong = nil

while true do
	if lastsong == songName.Value then
	-- ?????
	else
		lastsong = songs[index].Name
		sound.SoundId = "rbxassetid://"..songs[index].Value
		sound:SetAttribute("SongName", songs[index].Name)
		sound:Play()
		wait(sound.TimeLength)
		sound:Stop()
		wait(nextSongPlayTime)
	end
end

Here’s what you’ll want to do:

create a variable called something like ‘checkedSongs’.
at the start of the while loop. set it to be the same as songs.

inside the if where you put (-- ???) find the nextSong in the list (This post may help). Remove from what you put the nextSong in the checkedSongs, that way, checkedSongs has a list of all the songs, minus the last played.

Hello Mr_RainBowsYT

I have provided code that should give you exactly what you are looking for.

local sound = script.MusicPlayer
local songs = sound:GetChildren()
local nextsongplaytime = 2
local songname = game.Workspace.Scripts.MusicScript.MusicPlayer:GetAttribute("SongName")
--
while wait() do
	for i,v in pairs(songs) do
		sound.SoundId = "rbxassetid://" .. v.Value
		sound:SetAttribute("SongName", v.Name)
		sound:Play()
		sound.Stopped:Wait()
		wait(nextsongplaytime)
	end
end
1 Like

This might help. Run this function before doing the in pairs loop.

local usedSongs = {}
local musicFolder = game.ReplicatedStorage.MusicFolder:GetChildren() -- Path here
local nextSongWaitTime = 2

function clearQueue()
for i, v in pairs(usedSongs) do
table.remove(usedSongs, v)
end

local index = 0

while true do
 local randomMusic = musicFolder[math.random(1, #musicFolder)]
 if not table.find(usedSongs, randomMusic) then
   table.insert(usedSongs, randomMusic)
   randomMusic:Play()
   index += 1
   randomMusic.Ended:Wait()
   wait(nextSongPlayTime)
   end
  elseif index == #musicFolder then
  index = 0
  clearQueue()
  wait(nextSongPlayTime)
end
2 Likes

This looks beautiful should be exactly what OP is looking for.

2 Likes

Thanks everyone for your help ! I’m currently not zt home so i’ll try it later

your scripts look great but it’s not functionning , i’'l try to modify it tho

the scripts works but it just stops working after the first song

You have to remake it a bit, He utilizes a folder within replicated storage not a coded table within the script (your original script).

1 Like