Tables and parameters

Hey guys, I was recently reading a post on OOP(Object Oriented Programming) You can see it here for reference

[Concepts: Object Oriented Programming in Roblox]

I was reading and saw that he mentioned that when you add a Colon when calling a function from an inside constructor table, the table gets passed into the parameters, which allows changing a true/false value declared inside the table. what’s the difference between using a period and why does it not work with one? I find my biggest weakness in coding is understanding truly why things are the way they are.

1 Like

Coding sometimes isn’t the way of understanding just knowing.

Yeah, but why does it not work with a period? There is obviously something going on with how values are passed right?

Wdym by values passed? typefor30characterrule

Sorry, that was misleading. What I’m understanding from this is that when I call the function in the table using a colon, I’m essentially passing the table into the function , and if I use a period this would not work?

Oh so your making a table and making functions with the table inside of them?


Yeah, I’m trying to understand how this whole thing works.

That looks odd… Just send me the roblox studio version.

You could check the article, I linked it in my post.

Ight. Let me check it out! :smiley:

: passes self or the table of the module automatically instead of needing to get it yourself

1 Like

If you call a function using : the function itself must also be : otherwise parameters will be mixed and it will error

As @kingschool9 mentioned, calling a function with : passes self which is a variable that redirects itself to the table that you called it from. Calling functions from a dot(.) directly indexes the function. It’s recommended to only use the colon when you need the self argument

Example:

local tbl = {} -- the main table
tbl.var1 = 0 -- first property
tbl.var2 = 9 -- second property

function tbl:_add() -- method
   return self.var1 + self.var2 -- uses "self" to index the table so you can access the properties of it
end

function tbl.add(arg1, arg2) -- function
   return arg1 + arg2 -- return "arg1 + arg2"
end

print(tbl:_add()) -- prints "9"
print(tbl.add(7, 8)) -- prints "15"
3 Likes

This is very helpful, may I ask what programmers usually mean by index?

Index is usally when you do something like

local part = Instance.new("Part") -- create a new part
part.Name = "newPart" 
-- we accessed "Name" because the property's name is it's index or "key"

Or index can mean the directory to something

local workspace = workspace -- the workspace global
local part = workspace.Part -- the part
1 Like