When I do a print(slotContents) I get ‘{}’ returned on empty tables, but can’t do
if slotContents == {} then
As that doesn’t seem to work
for slotNumber, slotContents in ipairs(PlayersData.Inventory) do
if slotContents == {} then -- Empty slot
print("EMPTY") -- NEVER PRINTS
slotContents.Id = BlockId.Value
slotContents.Quantity = 1
ChangedSlot = slotNumber
end
end
When I print PlayersData.Inventory this is what I get:
So I want to be able to tell if a slot has an empty table, however if I do #slotContents, I always get 0, as the stuff inside the table is aranged as a dictionary, and thus using # won’t retrieve the amount of stuff.
Use pairs() if PlayerData.Inventory is a dictionary.
pairs() and ipairs () are functions that can be used with a for loop to go through each element of an array or dictionary without needing to set starting or ending points. pairs() is used with dictionaries, and ipairs() is used with arrays.
You can use table.sort, to sort lowest to highest.
Here’s a module I made that does this:
local SortModule = {}
function SortModule:HighToLow(Table)
table.sort(Table, function(a, b)
return (
a > b
)
end)
end
function SortModule:LowToHigh(Table)
table.sort(Table, function(a, b)
return (
a < b
)
end)
end
return SortModule