Append newest number into a table

Hello! I’m trying to append a new value to the end of this table:

local lobbies = {
	["1"] = {
		OwnerId = 1,
		Privacy = "P"
	},
	["2"] = {
		OwnerId = 3736935196,
		Privacy = "F"
	}--[[,
             ["3"] = { <-- What I want
                  OwnerId = 2,
                  Privacy: "F"
             }
         ]]
}

I tried doing this:

function lobbies.createLobby(ownerId:number, privacy:string)
	local lid = tostring(#lobbies+1) -- This is returning 0
	lobbies[lid] = {
		OwnerId = ownerId,
		Privacy = privacy
	}
	if config.AllowLogMessages then --config.AllowLogMessages = true
		print("Created lobby: lobbies["..lid.."]")
	end
	return tonumber(lid)
end

But tostring(#lobbies+1) is returning 0.

How would I do this? Is it as simple as a table.insert? (This code is in a module script gettings called by a script in ServerScriptService)

Oh boy, I haven’t done something like this in a while but let’s see how this goes. From memory… you should be able to do something like this:

table.insert(t, {
  Value = Value,
  AnotherValue = AnotherValue
})

Etc. (for reference t is an abbreviation for a table, just replace it with the name of your table)

You can’t get the length of a dictionary using the length operator, you would need to loop through the lobbies dictionary and increment every time it goes through something

Or alternatively you don’t need to do the whole lid thing and just directly append your lobby table to the lobbies table, it will automatically increment without you having to to do it, if you still want to return the lid, you’d just do #lobbies - 1

1 Like

Is there a way to get the key for which it is put in?

I’m not sure what you mean… could you reword that please?

1 Like

The table is formatted like this:

["1"] = {
	OwnerId = 1,
	Privacy = "P"
},
["2"] = {
	OwnerId = 3736935196,
	Privacy = "F"
}

And I want to get the ["3"] (or whatever number is next) part. Is there a way of doing this?

Ye-up!

Using a for loop will allow you to loop through this!

1 Like
for i,v in lobbies do
	print(i,v)
end

Console:
1 ▶{...}
1 ▶{...}
2 ▶{...}

What’s stopping you from using an array? Instead of using strings as keys, you can use numbers as indexes (so 1, 2, 3, not "1", "2", "3"). You’re able to use table.insert this way.

1 Like

Thanks! I used table.insert(t, {values}) and changed them from “1” → 1

#lobbies only works with arrays, I highly suggest to use numbers instead of numbers in string form for this.
Because you’ll have to first loop through all values to get the last value.

local function getLength(dict)
   local j = 1
   for i, v in pairs(dict)
      j += 1
   end
   return j
end

now we got the length of the dictonary.
To set it we will use:

lobbies[tostring(getLength(lobbies) +1)] = ...

Again, I highly suggest to just use a normal array instead of a dictonary with numbers in string from.

1 Like

Here is my final script I landed on: (btw I moved all lobbies into an array called AllLobbies)

table.insert(lobbies.AllLobbies, {
	OwnerId = ownerId,
	Privacy = "F"
})
local last = 0
for i,v in lobbies.AllLobbies do
	last += 1
	print(i,v)
end
if config.AllowLogMessages then
	print("Created lobby: lobbies["..last.."]")
end
return last

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