Keep in mind this is a general thought about datastores & saving data, I want to know what would be more efficient and load on the datastore for general uses and not only purchases.
Greetings fellow developers, this question/thought has come upon my mind countless times in the past month, I am interested to know what you guys think of this… Between 2 Developers, we both had a different opinion on which one would be more efficient and which of these is better to save on a datastore, let’s get to the question then:
Assuming that you can’t purchase the next tool without owning the previous, which is a better choice/more efficient to use(You can only own one of each tool)
A) Save owned tools in a table:
local toolTable = {"WoodenSword","WoodenPickaxe")
B) Save only the latest tool, with a string:
local latestTool = "WoodenPickaxe"
Dan wants to get all the tools he already owns but he also wants to purchase a tool, he wants to make sure it’s not a tool he already owns. StonePickaxe is better then StoneSword, Dan wants to purchase the StonePickaxe but he doesn’t own the StoneSword… Which one would better assist him in checking if the tool he currently owns stands one before the tool he wants to purchase?
These 2 examples are ones i’ve came across:
A) =
--Variables
local Items = game.ServerStorage.Items:GetChildren()
local InventoryExample = {"WoodenSword", "StoneSword"}
--Functions
function NextItem(Inventory)
local BestOwned, BestOwnedPrice = nil, 0
for i, v in pairs(Inventory) do
local ItemPrice = Items[v]:GetAttribute("Price")
if ItemPrice > BestOwned then
BestOwned, BestOwnedPrice = Items[v], ItemPrice
end
end
local x = nil
for i, v in pairs(Items) do
local ItemPrice = v:GetAttribute("Price")
if ItemPrice > BestOwnedPrice then
--update x
end
end
return --NextItem
end
print(NextItem(InventoryExample))
local item = {"GoldenSword"}
if NextItem(InventoryExample) == item and Items[NextItem(InventoryExample)] < Items[NextItem(NextItem(InventoryExample))] then
print("purchase right")
end
B) =
--Variables
local Items = game.ServerStorage.Items:GetChildren()
local LatestItem = ""
for i,v in pairs(Items) do
Items[v] = i
end
table.sort(Items, function(e, r)
return e:GetAttribute("Price") < r:GetAttribute("Price")
end)
--Functions
local function attemptPurchase(item)
if Items[LatestItem] + 1 == Items[item] then
print("purchase right")
end
end
Thanks for reading! feel free to share your opinion.