You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
1 - I want to create a function used for every table in a dictionary
What is the issue? Include screenshots / videos if possible!
2 - i couldnt find anything that would help me and be stable
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
3 - i saw the ezwrapper method thing but i really didnt understand it and i didnt want to use it i searched the whole developer hub for it
I assume you’re referring to when people use .__index to mimic OOP, although do be warned that this is more of an advanced topic.
I don’t know your skill level but if you’re still interested I highly recommend you watch this playlist all about OOP in roblox:
Here’s a little example of how it may be used (you will probably have to watch the playlist to understand the code below)
local module = {}
module.__index = module
function module.new()
local self = setmetatable({}, module)
self.Value = 5
return self
end
function module:doSomething()
print(self.Value)
end
ill explain further the reason im trying to make a “custom function” is i have a table and i have it somehow linked parents to it and it work with some tricks and its not cyclic and to get the parent i’d have to execute a function which is like
local table = {}
table.__index = table
--local plavceInTable = setmetatable{{}, table}
function placeInTable:Do()
print(self) -- empty
end
the line i made commented makes a really big problem, which is you cant do
PlaceInTable:Parent()PlaceInTable:Parent()
and the only way to actually use ur method could be a loop and then etc.
correct me if im wrong
i cant do that my table is a dictionary controlled by the player aka scripts and you dont manually add more tables into it so idk how to do what ur saying
When you use the “.” operator on a table, you are attempting to access an element within that table. This element can be a function, number, or string. However, when you try to access a function stored in a table using the “.” operator, you need to provide all of its parameters in the implementation and the function call . Sometimes, a function may take the table it is stored in as a parameter. In such cases, you can use the “:” operator. By using the “:” operator, you no longer need to explicitly pass the table to the function in both the implementation and the function call. This is because Luau automatically passes the table to the function.
example code
local mytable = {"foo" , "bar"}
-- implementation with "."
mytable.someFunction = function(self)
print(self) -- this will print {"foo" , "bar" , this_function_address }
end
-- implementation with ":"
function mytable:someFunction() -- luau passes self to this function
print(self)-- this will print {"foo" , "bar" , this_function_address }
end
-- calling with "."
mytable.someFunction(mytable)
-- calling with ":"
mytable:somefunction() -- i don't need to pass the table here
I know the “.” thing and the “:” but ill show an example of what i want
local mytable = {["Chocolate"] = {["Yum"] = {}, ["Bad"] = {}}}
--what i want to do
function ANYTHING:example()
--Do stuff
end
mytable.Chocolate.Yum:example
mytable.Chocolate:example
-- the function is applied to everything in the table