How can i make a custom function with ":" or even "." (function will be for a whole table)

You can write your topic however you want, but you need to answer these questions:

  1. 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

  2. What is the issue? Include screenshots / videos if possible!
    2 - i couldnt find anything that would help me and be stable

  3. 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

2 Likes

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
3 Likes

1 question is will this function be able to work on like other instances/tables?

I don’t know what you mean by that, yes it can store and manipulate instances and tables?

1 Like

OOP, make a class, and you can assign objects and functions into it.
No need for a module script by the way.

1 Like

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

Function(PlaceInTable)

but i want it like

PlaceInTable:Parent()

literally what bro just said

local table = {}
table.__index = table

local plavceInTable = setmetatable{{}, table}
function placeInTable:Do()
    print(self) -- empty
end
1 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

1 Like

wtheck are you talkin about :sob::sob:literally just repeat the same process

2 Likes

how do i explain and what didnt you understand :sob::sob:

1 Like

just make another metatable for this, its the same process

2 Likes

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

1 Like

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
2 Likes

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

how can i do the code above?

I found a solution which i did which trial and erorr its

	function FUNC(self)

			print(self)
		
	end
	local x = {["One"] = {Name = "Hi", Func = FUNC},["Two"] = {Name = "Hi", Func = FUNC}}
	x.One:Func()
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.