Looping A Function Back

Hello. In my code, there is a math.random() which calls a random piece of music from the folder RadioMusic. Everything else works, but I’m trying to find a way to make sure that the same song isn’t chosen twice. The idea is that it checks if it is, and if it is already playing then it will repick a number. Any help?

local plr = game.Players.LocalPlayer
local musicgui = plr:WaitForChild("PlayerGui"):WaitForChild("MusicGui")
local text = musicgui:WaitForChild("TextFrame").TextLabel
local imbutton = musicgui:WaitForChild("ImageButton")
local songfolder = game.Workspace:WaitForChild("RadioMusic")
local songs = songfolder:GetChildren()

imbutton.MouseButton1Click:Connect(function()
	imbutton.Interactable = false
	imbutton.ImageTransparency = 0.5	
	local music = songs[math.random(1, #songs)]
	print(music.Name)
	if music.Playing == true then
		-- How would I redo the number here?
	else 
		task.wait(1)
		for _, song in songs do
			if song:IsA("Sound") then
				song.Playing = false
			end
		end
		music.Playing = true
	end
	text.Text = (music.Name)
	task.wait(2)
	imbutton.Interactable = true
	imbutton.ImageTransparency = 0
end)

Right here is where these go…
One is; don’t pick the last song played.
The other is; play each song randomly once then start over.

local last
local music
repeat
	music = songs[math.random(1, #songs)]
until music ~= last
last = music
local picked = {}
local count = 0
local music

repeat
	if count == #songs then
		picked = {}
		count = 0
	end
	local idx = math.random(1, #songs)
	if not picked[idx] then
		picked[idx] = true
		count += 1
		music = songs[idx]
		break
	end
until false

Something like that anyways.

do an array.
Remove song from array so it doesnt get picked
When array becomes empty do table.clone() from a referance table and it all goes again.
You are overthinking.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.