So I am creating a pet system and right now I am working on a button that will automatically equip the best pets you have. All the pets a player owns are in a folder in their local player. Like game.Players.LocalPlayer.Pets . The way I want to determine which pet is greater than the other pet is by taking 2 number values inside of them, one of them is a coin multiplier, and the other is a diamond multiplier and I will take the average of these 2 values. I made a local script inside of the button on the pet inventory GUI that looks like this
local Player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local pets = Player.Pets:GetChildren()
for i, pet in ipairs(pets) do
end
end)
I left the inside of the for i function blank because I do not even know what to do.
local Player = game.Players.LocalPlayer
local Pets = Player.Pets
local HighestVal = -1
local BestPet = " "
script.Parent.MouseButton1Down:Connect(function()
for _, pet in pairs(Pets:GetChildren()) do
if pet.Value > HighestVal then
HighestVal = (pet.Coins.Value + pet.Diamond.Value) / 2
BestPet = pet.Name
end
end
Pets[BestPet] -- This is how to get the best pet
end
Hopefully this would work, if it doesn’t please let me know and I can try and fix it.
If you want to sort them, just use table.sort(t, sortingMethod). table.sort runs through a table, t, and sorts them based on sortingMethod. It basically calls the function with two elements and lets it decide whether they’re in the right order, that is, whether they should be {..., arg1, arg2, ...} or arg2 first. Here’s a quick example outlining how that could work for your usecase:
table.sort(yourTable, function(pet1, pet2)
-- some extra checks may be necessary, make sure pet1 and pet2 have everything in them first
local average1 = (pet1.Diamonds.Value + pet1.Coins.Value) / 2 -- get the average for pet1's diamond/coin mults, numbervalue names are probably different
local average2 = (pet2.Diamonds.Value + pet2.Coins.Value) / 2 -- same for pet2
return average1 > average2 -- the higher average goes first
end)
There were a couple of things wrong with the code, a player can 5 pets equipped at once so I don’t need to find one pet I need to find the top 5. I changed it up a bit and this is what I have now. I made it so that each pet has an actual value assigned to them.
local Player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local pets = Player.Pets:GetChildren()
for i, pet in ipairs(pets) do
local petValue = (pet.Multiplier1.Value + pet.Multiplier2.Value)/2
end
end)
If that is the case I would recommend going with @posatta method, as you can easily get the pet’s position in the table, so the first pet would be accessed like this Table[1] -- This will get the best pet It’s a far better method than the one I gave, since that only gets the highest pet.
I guess I used the wrong wording for my question because I don’t think my values are in a table. If you look at the most recent code I made
local Player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local pets = Player.Pets:GetChildren()
for i, pet in ipairs(pets) do
local petValue = (pet.Multiplier1.Value + pet.Multiplier2.Value)/2
end
end)
The values aren’t in a table so I just don’t know how I can sort those values or if there is a way to make a table every time the button is clicked and put those values in it.
First off, with the release of Instance Attributes | Roblox Creator Documentation I reccomend using attributes instead, as they are faster and accomplish the same thing. However, how you structure your game is not my decision so I’ll get to the coding.
Because you average the two values for a pet I would put the average into a table and then use the table.sort function. For example, I’d do something like this:
local function SortPets(pets)
local petTable = {}
for index, pet in ipairs(pets) do
table.insert(petTable, {pet = pet, value = (pet.NumberValue1 + pet.NumberValue2)/2})
end
table.sort(petTable, function(v1,v2)
return v1.value < v2.value
end)
for index, valueArray in ipairs(petTable) do
table.insert(petTable, index, valueArray.pet)
end
return petTable
end
Note: This code was rushed. There is most likely is a way to shorten it to make it more efficient and faster. However, it still works.