Inserting multiple values to the table with table.insert problem

  1. What do you want to achieve?
    I want to compare Multipliers of each pet to get pets with highest multiplier.

  2. What is the issue?
    When I print the table, table only contain one pet.

  3. What solutions have you tried so far?
    I tried to look on devforum but I dont really have much knowledge about table inserting so I wasnt able to figure out how to achieve it.

Heres my code:

local equipbesttable = {}
	
		local function insert(tbl,petidtotal,totalscore)
			table.insert(equipbesttable,{
				IDPet = petidtotal,
				Score = tonumber(totalscore),
			})
		end
		for i,pet in ipairs(Player:WaitForChild("Pets"):GetChildren()) do
			local total2 = tonumber(pet.Multiplier1.Value) + tonumber(pet.Multiplier2.Value)
			local totalpetid = pet.PetID.Value
			insert(equipbesttable, totalpetid, total2)
		end
		print("table inserted")	
		wait() table.sort(equipbesttable, function(a, b)
			return a.Score > b.Score
		end)
		print("table sorted")
		for i,v in pairs(equipbesttable) do
			print(i,v)
        end

Any help is appreciated thanks for reading :wink: .

Do you need this secondary table? Could you just do:

local pets = Player:WaitForChild("Pets"):GetChildren()

table.sort(pets, function(a, b)
	return a.Multiplier1.Value + a.Multiplier2.Value > b.Multiplier1.Value + b.Multiplier2.Value
end)

print("table sorted")

for i, v in ipairs(pets) do
	print(i, v.PetID.Value)
end

I don’t see anything immediately wrong with your code, though (except for the random wait() and tonumbers everywhere).

Are you sure that the print is working correctly? I know that printing tables has been buggy in the past.

Thank you, I will give it a try!

But How do I get the PetID now?

The same way you did before, what do you mean?

Oh yes sorry I get it now. :smiley:

I missed :GetChildren() but still same problem appear It only choose one pet.

I don’t know what to tell ya. Check that you actually have more than one thing in the player’s folder. I don’t remove items from the table anywhere so that’s sort of impossible.

I find out that the problem is appearing when I want to get the v.PetID.Value from the table. Table is now sorted perfectly.
So basically when I do print(pets)
it show me [1] = petwithbest, [2] = goodpet etc. but when I do print(v) It only prints the petwithbest.
How do I achieve all v not only the best?
Also thank you for your patience :slight_smile:

image

Code:

local petssorted = Player:WaitForChild("Pets"):GetChildren()
		print("table inserted")	
		table.sort(petssorted, function(a, b)
			return a.Multiplier1.Value + a.Multiplier2.Value > b.Multiplier1.Value + b.Multiplier2.Value
		end)
		print("table sorted")
		for i,v in ipairs(petssorted) do
			print(petssorted)
			print(i, v.PetID.Value)
				print("for i v")
        end

I don’t know – your screenshot cuts off before where the rest of the pets would print.

It works for me.

With this setup:

image

and this code in that script:

local Player = game.Players.LocalPlayer.PlayerScripts

local petssorted = Player:WaitForChild("Pets"):GetChildren();

print("table inserted")	

table.sort(petssorted, function(a: Pet, b: Pet)
	return a.Multiplier1.Value + a.Multiplier2.Value > b.Multiplier1.Value + b.Multiplier2.Value
end)

print("table sorted")

for i,v in ipairs(petssorted) do
	print(petssorted)
	print(i, v.PetID.Value)
	print("for i v")
end

I get this output:

image

…Which is what I expect.

1 Like

Thank you so much I am marking it as a solution.

Hey Mike I tried it in Local Script and it works as well but… this script is Normal script (Server script) inside of ServerScriptServices and it for some reason only prints the best pet.

I am not sure what output you’re expecting, that output looks correct to me.

Basically I have local script that is invoking server and it sends Player variable to the Server Script and in the server script its sorting the table. When I print sorted table inside the Server script (green color) it shows only the best pet but when I print same table inside of the local script (blue color) it shows every sorted pet inside of the table.