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
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.
I think your solution may work but I have no idea how to script this 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
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.
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
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