Can anyone give a good explanation about tables?

i haven’t seen good explanations. i know hey are basically variables with multiple lines that get stored. i want to make a loading system but i don’t want to go down the elseif route everyone is avoiding with tables.

Something like that?

local Options = {}
function Options.A()
	print("Option a")
end
function Options.B()
	print("Option b")
end


local Target = "A"
local Handle = Options[Target]
if Handle then
	Handle()
else
	warn("Option not found:", Target)
end

and with elseif

local Target = "A"
if Target == "A" then
	print("Option a")
elseif Target == "B" then
	print("Option b")
else
	warn("Option not found:", Target)
end
1 Like

no. it’s like having a table of guns or a shop with costs and calues and crap. i for some reason don’t really understand how that’s gonna lower the lines.

It’s not about lowering the line count, it’s to organize your code and make it look cleaner and more modularized. The general convention in Roblox is to store valuable game data within ModuleScripts, which are just tables.

Tables are mutable, meaning that they can change during runtime, while the source code can’t. Pretty big difference between using tables and chained else-if statements.

It’s easier to edit a table than a long else-if chain. You just input the necessary information, whereas adding an entry to an else-if chain also means adding all the functions needed to process the data.

By using tables, you can annotate its type which can help prevent you from making silly mistakes such as defining the wrong data.

type gunConfig = {name: string, damage: number, price: number}
local gunConfigurations: {[string]: gunConfig} = {
  gun1 = {name = 'test gun', damage = 10, price = 2000},
  gun2 = {name = 'also test gun', damage = 10}, --the script analysis will highlight this line as problematic because it's missing price.
}

Using a table just makes more sense.

1 Like

Thanks! now, could you show me an if statement for it? that screws me over.

For what particularly? If tables are used just to store data, if statements aren’t really necessary, only to check if the data actually exists.

local data = {a = 1, b = 2, c = 3, d = 4}
data.e = 5
data.f = 6

local function foo(str)
  local v = data[str]
  if v ~= nil then --check if value exists
    return v
  else
    warn('data not found')
  end
end
1 Like

i meant checking for the values. like if you buy something it needs the check for the price value thingy.

Just index for the price and then do a comparison of it

local items = {apple = 10, banana = 20, chryslerBuilding = 1e6}

local function buyItem(item, balance) --balance is how much money you have
  local price = items[item]
  if price == nil then return false end --item doesn't exist, return false
  if price <= balance then
    return balance - price --return the remaining balance
  end
  return false --can't afford item, return false
end

If you still don’t understand tables then you can refer to the official Lua tutorial
https://www.lua.org/pil/2.5.html

1 Like