@Dominus_Marceau these are metatables in a nutshell.
You will hear the sentence “metatables are very powerful” very often. That’s because they are always useful. When you start to work with OOP and data, tables are very common and so are their helpful sidekicks, metatables. You can think of them as tables that include special functions to change behaviour of the table they are appended to. They hold configurations for that table and let you add functionality to it. There is a big number of so called metamethods, about which you can read here (amazing post by @starmaq).
Let’s look at an example:
--[[
Welcome to the RENT A BloxyCAR! This is an automated
machine, helping you complete your holiday!
We have four cars you can rent here:
]]
local cars = {"BloxAudi", "RoPorsche", "BloxFord", "Rotsubishi"}
--[[
-- "This is absolutely nice, but I'd like to
drive a RBX_BMW. In fact, I love it! Is there
any way I could rent it here?" --
Unfortunately, that's not possible, as we don't
have any RBX_BMW's for rent. Lucky for you, we have
a metatable implemented!
[!][!] Special deal ahead [!][!]
RANDOM CHOICE:
get a ROTOYOTA or a FERRORI !
]]
local cars = setmetatable(cars, {
__index = function(table, key)
-- match is equal to true if both random numbers match
local match = math.random(1, 10) == math.random(1, 10)
return match and "Ferrori" or "RoToyota"
end
})
-- If the element is not part of the table,
-- prepared key is returned instead of nil.
print(cars.RBX_BMW)
__index may look confusing at first, but is actually not scary and is one of many useful metamethods.
@sjr04 and @HugeCoolboy2007 really took time to write nice posts explaining this further.
Now back to your case. Why does this work…
if sentence:match('brick') then end
…and why doesn’t the following (?):
if sentence.match('brick') then end
Because, as stated before, when colon is used, a table that called the method, which is in our case :match, is automatically past as an argument, a parameter to that function. When dot is used, parameter has to be manually entered, which means that is former example, string.match method has no string to try to find word “brick” in.
This would work:
local sentence = "Who doesn't like brick battle?"
if sentence.match(sentence, 'brick') then
print("Match found!")
end
But it seems pointless to call a method with a table and then manually pass it as argument again. That’s why we write string.match() instead. String functions are all part of the same standard lua string library.