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

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

thanks actually I don’t know if it’s another script but I modified things in my game and the songs plays normally

1 Like