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

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

Yes I saw that, but it’s something wrong inside the while true do loop i’m going to try with a lastSong variable

local sound = script.MusicPlayer
local songs = sound:GetChildren()
local nextSongPlayTime = 2
local songName = workspace.Scripts.MusicScript.MusicPlayer:GetAttribute("SongName")

local oldSong
while true do
    local music = songs[math.random(1,#songs)]

    if oldSong == music.Value then continue end -- not sure if the continue part works

    oldSong = music.Value
	sound.SoundId = "rbxassetid://"..music.Value
	sound:SetAttribute("SongName", music.Name)
	sound:Play()
    sound.Ended:Wait()
	wait(nextSongPlayTime)
end
1 Like

what is the error outputting to you? (assuming there is)

it doesn’t have any errors but the song doesn’t play, but I did modify a bit his script since roblox studio detects some typos ; image

there’s only other script outputs

local usedSongs = {}
local musicFolder = game.Workspace.Scripts.MusicScript.MusicPlayer:GetChildren()
local nextSongPlayTime = 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)
		
		elseif index == #musicFolder then
	index = 0
	clearQueue()
	wait(nextSongPlayTime)
		end
	end
	end

oh wait your script might work i’m checking (thanks it works ! also I didn’t knew that continue exists in lua lol) oh ouch it doesn’t play the song after rip that’s strange cur it worked with 5 second wait instead of sound.Ended:Wait() i’m confused

1 Like

I’ll try to make this and post a file some time later because I really think it is effective

1 Like

oh ok well thanks for helping me !

I’m pretty sure that Sound.Ended fires once a song has finished.

Try replacing sound.Ended:Wait() with repeat wait() until not sound.IsPlaying or with wait(sound.TimeLength).

1 Like