Question about tables

Hi!
I’ve seen this code on the devHub and I don’t fully understand it.

local Module = {}

module.createLightning = function(startPos, endPos, segements)

or this one

local productFunctions = {}
-- ProductId 123123 for a full heal
productFunctions[123123] = function(receipt, player)

Are those like table.insert? Wouldn’t this error because there is no productFunctions[123123], the table is empty.?

This wouldn’t error because it’s simply making that key have whatever’s on the other end of the = as a value so when you do module.createLighting() it won’t give you an error it’ll fire the function that’s been assigned as the value.

1 Like

Oh, so whenever calling productFunctions[123123], that function would run?

Yes, that’s exactly how it works.

1 Like

So are those functions stored inside of the table though, or is it just like with any other variable, just like doing local idk = function(receipt, player), because making a table would make no sense, then.

it’s actually just like making a variable with a function, except the reason it’s in a table is because in your example it’s in a module script, which usually returns a table, so other scripts can usr the variables/functions inside of that table.

1 Like