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)
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
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.
#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.
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