How to store more objects in Table?

Im trying to make a table to look like this

{[Username] = {[1]["Resource"] = ["Number"] = 10, ["Location"] = location}
              {[2]["Resource"] = ["Number"] = 10, ["Location"] = location}
}

I tried:

Table = username
table.insert(Table[Username], {[Resource] = {
["Number"] = Number, ["Location"] = location}})

But it doesnt work

this is not valid syntax

i think you meant

{
  [Username] = {["Resource"] = {["Number"] = 10, ["Location"] = location}
}

to do so, you can do

data[Username] = {
   ["Resource"] = {
    ["Number"] = Number,
    ["Location"] = location
  }
}

just realized you want to store 2 of the same keys, you cant do that

hmm

1 Like

my bad, I meant to put “it doesn’t work”

{[Username] = {[Resource] = {["Number"] = 10, ["Location"] =  location}},
              {[Resource] = {["Number"] = 10, ["Location"] =  location}},
}

If the [“Number”] is above or equal to 10, it will make another key inside the players table. That’s what I’m trying to do

data[Username] = {
   ["Resource"] = {
    ["Number"] = Number,
    ["Location"] = location
  }
}

This works if the player doesn’t exist inside the table. This what happens when I print the table

{["itzMehPlayin"] = {[Resource] = {["Number"] = 10, ["Location"] = location}}}

But if I want to add another value inside the players table since the [“Number”] is equal to 10. It overwrites a value inside the players table

Sample:

print(Data) -- Prints the table
{["itzMehPlayin"] = {[Instance(1)] = {["Number"] = 10, ["Location"] = location}}}

-- After adding a new value

{["itzMehPlayin"] = {[Instance(2)] = {["Number"] = 10, ["Location"] = location}}}

It just overwrites

I don’t exactly know what you’re trying to do but it seems like you’re trying to implement a inventory system with a fixed resource limit. Heres how you can do this

local resourceLimit = 10

local data = {
  ['itzMehPlayin'] = {
    {
       Instance = resource; -- the resource object
       Number = 10;
       Location = location;
    };
  };
}

local resourceTemplate = {
       Instance = nil; -- the resource object
       Number = 0;
       Location = location;
};

local function addResource(playerName: string, resource: Instance)
   local resources = data[playerName] -- get the current resources

   for _, resource in resources do
      if resource.Instance.Name == resource.Name and resource.Number < resourceLimit then
         resource.Number += 1 -- increment the resource by
         return -- stop the function
      end
   end

   local resource = table.clone(resourceTemplate) -- clone the template
   resource.Number += 1 -- increment the resource by 1

   table.insert(resources, resource) -- insert the resource to the player's resources data
end

addResource('itzMehPlayin', instance)

This is untested but it should work

I changed the resource layout because you can’t have 2 of the same keys

1 Like

What does " : " and " ; " do? Can I just use " , " instead of those?

1 Like

It’s just another style option, yes you can just use , if you wish to do so. And regarding the : it’s for type checking, for instance when you paste it in Roblox you’ll be able to see how when you call the function it’ll tell you more info.

1 Like

Look at the text highlighted in yellow:
image

This formatting helps the user know what they need to input, just like other Roblox functions do:

After some tweaks, I made it work. Thank you!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.