Hello!
Recently I’ve been making a game and was thinking a way to make an equip best pets system and while i was trying to do that, i realized that the system i am trying to make had topics that i had yet to learn about, and so I did (in a new game). Now I wanna know if there is any way my code could improve and be more optimized;
I wonder if there is any better way to do this with less “tables” i guess? Or if there is any other system that does the same but better
BestPet.lua (1.6 KB)
Otherwise here’s the code
local config = {
Dog = {
Common = {
Money = 1.1,
},
Name = "Dog"
},
Cat = {
Common = {
Money = 1.3
},
Name = "Cat"
},
Dragon = {
Common = {
Money = 1.5
},
Name = "Dragon"
},
FlyingCat = {
Common = {
Money = 2
},
Name = "FlyingCat"
},
Lion = {
Common = {
Money = 20
},
Name = "Lion"
}
}
local ownedPets = { --dictionary
--["Dragon"] = 1.5,
}
for _, pet in pairs(config) do
ownedPets[pet.Name] = pet.Common.Money
end
function Length(dictionary)
local counter = 0
for _, _ in pairs(ownedPets) do
counter += 1
end
return counter
end
function Best(dictionary)
local counter = 0
for k, v in pairs(dictionary) do
if counter < v then
counter = v
end
end
return counter
end
function Index(dictionary, best)
for k, v in pairs(dictionary) do
if v == best then
return k
end
end
end
local bestPets = {}
local maxEquipped = 4
local ownedPetsAmount = Length(ownedPets)
if ownedPetsAmount < maxEquipped then --if player has less pets than the amount they can equip
for counter = 1, ownedPetsAmount do
local best = Best(ownedPets)
local index = Index(ownedPets, best)
table.insert(bestPets, index)
ownedPets[index] = nil
end
elseif ownedPetsAmount >= maxEquipped then
for counter = 1, maxEquipped do
local best = Best(ownedPets)
local index = Index(ownedPets, best)
table.insert(bestPets, index)
ownedPets[index] = nil
end
end
print(bestPets)
print(ownedPets)
--clear for next use
table.clear(ownedPets)
table.clear(bestPets)
The reason why i have an array (i think thats what it is) named config is because in my original game i have a module script that contains all the pets’ data (Though without the Name string and not every pet is Common). Edit: forgot to mention that this is supposed to be in the server side with some of the information passed by the client through a remotefunction (inventory gui equip best button)