"attempt to yield across metamethod/C-call boundary" error, I have no clue what it means

recently I started getting this error:
image
and I have 0 clue what it means. it seems to happen at random, and it only happens when a player joins the game. I am 90% sure it is in relation to this function:

local function HighestPlrSort()
	local PList = game.Players:GetPlayers()

	local function SortFunc(P1, P2)
		if not P1:FindFirstChild("leaderstats") then
			P1:WaitForChild("leaderstats")
		end
		if not P2:FindFirstChild("leaderstats") then
				P2:WaitForChild("leaderstats")
		end
		return P1.leaderstats["Hit's"].Value > P2.leaderstats["Hit's"].Value
	end

	table.sort(PList, SortFunc)

	return(PList[1])
end

those are the two fuctions in the error, and the top line in the function is at line 3, the end for the function is at line 19. here is line 8:

P1:WaitForChild("leaderstats")

here is line 16:

table.sort(PList, SortFunc)

(both of these are inside the function pictured above) and here is line 47:

Plr = HighestPlrSort() 

I do not know what the error means, can anyone help?

1 Like

The sorting function is not allowed to yield, so no wait, WaitForChild, task.wait, etc.

how can I replace

if not P1:FindFirstChild("leaderstats") then
			P1:WaitForChild("leaderstats")
		end
		if not P2:FindFirstChild("leaderstats") then
				P2:WaitForChild("leaderstats")
		end

then? around 30% of the time when a new player joins there leaderstats are not loaded right away, so I need to wait for the leaderstats.

1 Like

I think I found a solution :wink:

if not P1:FindFirstChild("leaderstats") then
			repeat 
				P1:FindFirstChild("leaderstats")
				task.wait()
			until 
				P1:FindFirstChild("leaderstats")
		end
		if not P2:FindFirstChild("leaderstats") then
			repeat 
				P2:FindFirstChild("leaderstats")
				task.wait()
			until 
				P2:FindFirstChild("leaderstats")
		end

not sure how efficient it is, but it works. still gonna mark your post as solution though as without your post I still would have no clue what the error meant, thanks.

1 Like

Lots of ways you could go about it. The one that’s closest to what you’ve got is to make HighestPlrSort wait for all leaderstats to load before sorting the players. Although that’s a bit weird, it doesn’t make a lot of sense for the sorting function to also verify that the data it’s supposed to sort is valid.

I would create another function for that, called IsLeaderstatsReady(p: Player) and perhaps another called WaitForLeaderstatsReady(p: Player). Then I would make it so whatever function is calling HighestPlrSort is responsible for checking that all leaderstats and whatever else is ready, or perhaps just skip the players whose stats aren’t ready.

1 Like

I never thought about skipping the players whose leader stats are not ready, but that would work even better then what I’m doing now. I think making a function that returns a table with all the players that have leaderstats (like you said) is the best way to go about this. Thanks for an even better solution, marking your new post as solution as if fits better.

1 Like