Creating A Table For Items

I’d like to use a table to assign the items I have with an ID so I can save them without using StringValues to save space.

For example
Wood, ID 1
Apple, ID 2
Box, ID 3

How would I go about doing that? I looked it up and it’s left me a bit confused. Like how would I know Wood is assigned to the number 1?

you can just do this,

local items = {
    [1] = "Wood" -- or you can store Instance, for example 'game.ServerStorage.Wood'
    [2] = "Apple"
    [3] = "Box" 
    -- id can be either number or string, depends on what you want.
}

and then, whenever you want to access those items you can just do this,

local items = {
    [1] = "Wood"
    [2] = "Apple"
    [3] = "Box"
}
local selectedItem = items[1] -- you can replace 1 with any id you want of course.

now, if you want to know what ID ‘Wood’ if assigned to then, you can make a loop and then whenever you have your target item, you check the index (or the key, actually), something like this,

local items = {
    [1] = "Wood"
    [2] = "Apple"
    [3] = "Box"
}

local targetItem = "Wood" -- the item you want to check what the ID is.
local itemID -- you won't assign this since you don't have the value, yet.

for id, item in pairs(items) do
    -- id is the key
    if item == targetItem then
        itemID = id
    end
end

print(itemID) -- now you know what ID 'Wood' is.
5 Likes

Just store all of your stuff in a folder located in replicated storage
and use Folder:GetChildren --getChildren always returns a table of the children

1 Like

You can use module scripts for this, its not really hard to script and very easy to use for updates. Feel free to message me for any help with this.

I would recommend using Dictionaries since you can save a value, table, dictionary with a named key. this way you can save a value to ‘Wood’ like the amount that the play is holding for example

['Wood'] = 1, and so on.

Dictionary = {
  ['Wood'] = 1,
  ['Stone'] = 3,
  ['Dirt'] = 20
}

Here is Roblox’s page about Dictionaries
https://education.roblox.com/en-us/resources/intro-to-dictionaries---series

1 Like

I wouldn’t recommend doing that especially when saving data - for example you misspell item name and want to change it without players loosing their data the answer is you can but it is more complicated than storing only the number index and adding name of the item inside of the table and then use it to display the name.

that’s cap.
dictionaries are better, they even prevent “duping” lol

1 Like

i save plr data in big arrays with complex data like that all the time and i think its more convenient and reliable knowing i dont have 2 tables one for names and one for values

Arrays “prevent” duping as well you can’t have same number indexes in same table as well.

you should do some research.

local a = {[1]='a',[2]='b']}

is equal to

local a = {'a','b'}

I get what do you mean but it’s better to store number index instead of string because you can’t change the misspelled name without players loosing their data or adding extra piece of code to convert all existing data with misspelled name.

Then just don’t spell it wrong.

you literally can “correct” misspelled stuff, which you shouldn’t type wrong in the first place…

Yes I know? That’s my whole point

local tab1 = {[1] = "hey"}

tab1[1] = "hello"

print(tab1[1]) -- prints "hello" not "hey"

local tab2 = {["Hey"] = 1}

tab2["Hey"] = 0

print(tab2.Hey) -- prints "0" instead of "1"

As you can see both arrays and dictionaries have same behaviour of overwriting.

People aren’t robots we all do mistakes from time to time and you can simply overlook something, it just happens without us realizing.

@WhitexHat

and your point? I don’t get it.

how is

local t = {'Wood','Wood','Wood','Wood'}

better than

local t = {['Wood']=4}

A little off-topic but I would create a test place for testing table saving or data handling before putting it into your real game.

The thing is when you want to save data like that you shouldn’t do it this way you should instead do this

local itemTable = {
  [1] = {Name = "Wood", Count = 4}
}

And instead of saving whole “Wood” string you are gonna only save the number index 1 which takes a lot less space in datastore.

And when you want to display name “Wood” you will just do itemTable[numberIndex].Name

OP is unlikely to have maybe more than a dozen or so items. Doing a spell check may take at most five minutes?

Unless he’s planning the next Bloxburg of course.

how can you even misspell? :rofl:

--// database, modulescript
return {'Wood'};
--// server stuff
remote.OnServerInvoke = function(Player, Item)
assert(type(Item) ~= 'string', 'not a string'));
assert(table.find(Database, Item), 'could not find item');
--// whatever ur code is..
return 'item exists'; --// example
end;
--// client stuff
local item = remote:InvokeServer('Wood'); --// errors if it doesn't exist, so you know where you misspelled it if u want lol