[Question] Tables in LUA

Hello guys,
I am new coding and I am trying to know more about tables in Lua and I would like to know How Can I have a entries number ?
If I have got this code :

Resource A server.lua

local table_t = {
    ["example1"] = {},
	["example2"] = {},
AddEventHandler("table:insert", function(x)
    table.insert(table_t[x], source)

Resource B   server.lua


function table_counter()
  local count = 0
  local x = "example1"
  for _ in pairs(table_t[x]) do
  count = count + 1 
  return count

How can the function table_counter has access at table from Resource A?


You can export a function to retrieve it or use events. If it’s in the same resource you can access it anyway.

In FiveM , all functions from script “lua” is global (cause you can use local function (to acces only in the file)) , but your TABLE must be “GLOBAL” so remove “local” in your table creation in server.lua Source A.

table_t = {
[“example1”] = {},
[“example2”] = {}

With that table_t is “Global” now

Remove the last , it’s better for table too

1 Like

Are we agree that table cannot be accessible in server if declaring in client ?

Forget tables. They’re fucking awful. For an array, use a for loop. Pass in the data.

AddEventHandler(“table:insert”, function(x)

Make that, function(x, y, z, color, vehID)

And what is the table for? If it needs to be on both server/client, you should be using a database.

You have a table_t on 24 clients, and WTF is the server suppose to have? Wait the clients are sharing the same table now too? Dafuq are you doing. It makes no sense. You want a global table synced between 24 players and the server. Yeah that’s called a database. Not LUA shit tables. Don’t use fucking tables. Slow as shit too.

humm… first off calm down. I’m not an expert like you are.
And yes I need synced datas client/server all 5 seconds. So I’ll use this, not Db.

So we have client a, client b, server. Server is master table? When updates made to master table you send the new table to everyone connected? How often are clients updating it? This can get complicated and messy quick.

1 Like

Uhm, I wouldn’t exactly go as far as to say tables in Lua are slow. It depends on how you’ve set them up and how you’re using it. Yes, using pairs or ipairs can be a bit over the top if it has to iterate over a very large amount of data.

But a regular for loop is actually rather fast obviously not as fast as for example an array in C++, but you can’t really compare the two languages.

However, I do agree that if he wants to update tablas across all clients every 5 seconds - he’d be better off using a database. Of course it’s doable, performance impact shouldn’t be too noticeable so long as it’s done properly and tables don’t contain huge amounts of data.

Not sure why you seem to be so anti-Lua :stuck_out_tongue:

I like Lua. But for FiveM modding, you don’t really need tables. People are misusing them, not understanding them, and doing shit they shouldn’t. you have a database. you have arrays. you have for loops. in some of the scripts its like people are getting paid to use ipairs as much as freaking possible. if we scanned all the lua mods here, the odds theres someone iterating over a table and inserting new entries into that table? id guess pretty likely.

i mean yes tables are fine. they’re only slow if misused. he just wanted to use tables as a global database and I really didn’t want him trying to do that. :slight_smile:

Ah got you, then we’re on the same page. :blush:

How to use them proprely then ? @siggyfawn

. What exactly are you trying to do?

I got

allZone = {
  {name = "string1", coords = { {x = x1, y = y1, z  = z1}, {x= x2, y = y2, z = z2} ,etc...    },
  {gravityCenter = {x = x1, y = x1} }, 
  {longestDistance = int1}
  {name = "string1", coords = { {x = x1, y = y1, z  = z1}, {x= x2, y = y2, z = z2} ,etc...    },
  {gravityCenter = {x = x1, y = x1} }, 
  {longestDistance = int1}

Do’nt hit me please, I need to get all that infos on a zone with a infinity number of zone