"Invalid order function for sorting" error when attempting to prevent songs from repeating

Hello. I am trying to create a music player that does not repeat songs in a row. When using math.random, a song ended up playing 5 times in a row, which is not what I want. I want a music player that will not repeat songs.

I used this post as reference for my script

However when I used the solution in the post, I got the same error as shown in the title, so I attempted to edit it, yet I still get the same error.

I don’t know what went wrong?

Here is my code:

math.randomseed(tick()) 

local AvailableMusic = {9043591515,9043592895,1842259478,1836891661,1841984324,1848354536,1843041874,9040291273,1838834495,1838091800}

while (#AvailableMusic > 0) do
	print("shuffling playlist...")
	table.sort(AvailableMusic, function()
		return Random.new():NextNumber() < 0.5
	end)
	for track, song in ipairs(AvailableMusic) do
		script.Parent.SoundId = "rbxassetid://"..song
		script.Parent:Play()
		script.Parent.Ended:Wait()
		wait(2)
	end
	print("end of playlist reached.")
end
1 Like

I’ve solved a similar post before. You can probably get the desired result by using my code as a reference.

Regarding the reason your code is not working, I think it’s because you randomly return the true and false values on this line:

I recommend you use my way of randomly selecting songs without choosing the same one in a row.

1 Like

I tried out your solution, and it seems to work at first however it only plays one song. It does not play the next song. I’m not sure if I may have missed something or not, please let me know. Here is my code:

math.randomseed(tick())

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local audios = game.ReplicatedStorage.Music:GetChildren()

local feed = {}

local function getAudio()
	return audios[math.random(1, #audios)]
end

while true do
	local current = getAudio()
	
	if table.find(feed, current.Name) then
		repeat current = getAudio() task.wait() until not table.find(feed, current.Name)
	end

	table.insert(feed, current.Name)

	if #feed > math.floor(#audios/2) then
		table.remove(feed, 1)
	end
	
	current:Play()

	current.Ended:Wait()
end

I can’t tell what’s the issue, so you have to add some print functions to see where’s the problem.

Btw, you can replace line current.Ended:Wait() with

task.wait(3)
current:Stop()

to not wait for the song to end.

math.randomseed(tick())

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local audios = game.ReplicatedStorage.Music:GetChildren()

local feed = {}

local function getAudio()
	return audios[math.random(1, #audios)]
end

while true do
	print("current feed:", feed)
	local current = getAudio()
	print("\nselected song:", current)
	
	if table.find(feed, current.Name) then
		repeat current = getAudio() task.wait() print("song already played") until not table.find(feed, current.Name)
	end

	print("adding song to the feed")
	table.insert(feed, current.Name)

	if #feed > math.floor(#audios/2) then
		table.remove(feed, 1)
	end
	
	current:Play()

	print("the song is playing, waiting")
	current.Ended:Wait()
	print("the song has stopped playing")
end

@crumbsinmytoes, looking at your code, I think that the issue is somewhere outside the script. Although, that may not be true.

1 Like

So I ended up creating a fresh game, just a blank baseplate, since you said the issue may be outside the script. I moved the music folder and the script that you gave me into the blank game and the same issue is still happening. Plays one song, then doesn’t play the next. I don’t understand why it is not working, because it did print saying the next song was playing however I could not hear it.

image

Muzak Maker is the second song that should have been playing, yet I could not hear it.

It works just fine for me.

Here’s my place file:
music.rbxl (43.2 KB)

As I mentioned here, it’s probably something outside the script.

1 Like

Huh, weird that it didn’t work for me. I used your rblx and switched your songs out with my songs and it works perfectly. Thank you so much! Sorry for the trouble.

1 Like

See my post about random unique selections in general: How to select N random items from a list (Example)

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