Attempt to index number with number

local Table = {
"Mag", 2,
"Mag", 3,
"Mag", 1,
}
local Sorted = table.sort(Table, function(A, B)
	return A[2] > B[2]
end)

for _, Sort in ipairs(Sorted) do
	print(Sort)
end

Cant seem to fix the problem any ideas?

2 Likes

You lack braces around your sub-tables.

local Table = {"Mag", 2, "Mag", 3, "Mag", 1}

This is what your table looks like right now, so each element is being iterated separately. For desired behavior, what you want is {{"Mag", 2}, {"Mag", 3}, {"Mag", 1}}.

Also, table.sort sorts the table in place and does not return anything, so you shouldn’t have to do local Sorted =.

4 Likes

Ok, I made another script that attempts do do the same thing, but im pretty sure its in {}

for _, Node in ipairs(CS:GetTagged("Node")) do
			print(Node.Parent)
			
			if CS:HasTag(Node.Parent, "Roads") and Node.Parent.Parent.Name == "Workspace" then
				local Mag = math.floor((Node.Position - Car:GetPrimaryPartCFrame().Position).magnitude)
				table.insert(Nodes, #Nodes+1, {Node, Mag})
			end
		end
	end
	
		for _, Mags in ipairs(Nodes) do
			local Sorted = table.sort(Mags, function(A, B)
				return A[2] > B[2] 
			end)

It gives the same exact error.

You’re iterating over Nodes, which contains pairs in the form of {A, B}. You then call table.sort on those pairs. Maybe what you want to achieve is actually table.sort(Nodes, ...etc)?

2 Likes

I fixed it, I rewrote it the way that my first script looked. Thanks for help!

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