What do you want to achieve?
I want to compare Multipliers of each pet to get pets with highest multiplier.
What is the issue?
When I print the table, table only contain one pet.
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
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 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
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:
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
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.
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.