Creating A Table For Items

are you testing stuff before leaving a reply?

local http = game:GetService('HttpService');
local itemTable = {[1] = {Name = "Wood", Count = 4}};
warn(#http:JSONEncode(itemTable)); --> 27

local dataTable = {['Wood'] = 4};
warn(#http:JSONEncode(dataTable)); --> 10

also what u said might have lots of issues.

I don’t really care if you going to check everything you wrote during making update or whatever… my point is that you can easily change it and you don’t need to check everything because the most important for all of us (or at least I think) is time… in meanwhile you can do a lot more things than checking if you made any mistake.

you can’t make mistakes.

from what you’re saying, you clearly don’t know anything about server side sanity checks, which means your game is easily exploitable

Who would save the whole table :Dd the only necessary thing is the item identificator and that’s 1 in this situation, because you need it to access the data from table that aren’t suppose to be saved.

You can make mistakes and everybody on this planet makes mistakes, if you tell me that you aren’t making mistakes then you are lying to yourself.

How does naming items have something to do with sanity checks? I am just saying what are the best practices when working with tables and data.

at this point I think you’re trolling :sweat_smile:

I have not read the entire argument, but what do dictionaries have to do with running server sided sanity checks (Which are useless nowadays)

Thank you all for your answers! I’ve marked the one that was the most helpful(and earliest) to me. I’m sure they’re all valid answers though :slight_smile:

Because you need to put , at the end of each line in the table

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

Sorry for being such a pest but would it be possible for me to assign another string value to that?

For example a category, wood is material, apple is food, box is object

Or do you have any suggestions how I’d categorise things?

I was thinking of just spacing out IDS so 1 - 99 is materials, 100 - 199 is food etc… does that sound sensible?

For me its easiest way to sort items type

local items = {
["mats"] = {};
["food"] = {};
["Something u would like to be here"] = {};
}

Yes you can of course do it like that:

local items = {
    [1] = {Name = "Wood", Id = 20},
    [2] = {Name = "Apple", Id = 150},
    [3] = {Name = "Box", Id = 142}, 
}

and if you want to get the item name you will just do

print(items[1].Name,items[2].Id) -- and so on
1 Like

I love how you basically made “Box” a food lmao

1 Like

About finding Wood without the ID

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.

If I’ve got a-lot of values, I’m talking 100s, is this still reliable? Would it take too long or cause any problems if I had to individually check each value?

Sorry if these seem like stupid questions.

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

print(items[1]) -- "1" is the id, this is how you get the item
print(table.find(items, "Wood")) -- "Wood" is the item, this is how you get the id

you can do this without a loop

oh yes, i’m sorry. you can just use table.find() and it will return the index of the item you’re looking for

local itemid = table.find(items, "Wood")