I need help with creating function inside module script

API={}
API.PORTIONS={
	BUY=function(X)
		print("BUYING",X)
	end,
	PLAYEROWNS=function(Y)
		print("PLAYER OWNS",Y)
	end,
}
API

so i am playing around the module script and I need help to do

API.PORTIONS:BUY(X)
--instead of
API.PORTIONS.BUY(X)

I want to add the function inside the table so that it is easy to manage.
which means I don’t want to add function like

function API.PORTIONS:BUY(X)
--i don't want this
end

please help As soon as possible…

local module = {
	
	fun=function() end,
}
return module
2 Likes

A metatable could be of use for this.

1 Like
API={}
API.PORTIONS={
	BUY=function(self, X)
		print("BUYING",X)
	end,
	PLAYEROWNS=function(self, Y)
		print("PLAYER OWNS",Y)
	end,
}

?

Are you trying to access the other functions as well?

1 Like

actually when i do this with: the first argument it passes is

self

that’s why i was confused

yh i just realised ty

This text will be blurred

local API = {}

API.PORTIONS = {}
local PORTIONS = API.PORTIONS

setmetatable(PORTIONS, {
	__index = function(t, k)
		if k == "BUY" then
			return function(self, ...)
				print("BUYING", ...)
			end
		elseif k == "PLAYEROWNS" then
			return function(self, ...)
				print("PLAYER OWNS", ...)
			end
		end
	end
})

return API
local API = require(script.Meta)

API.PORTIONS:BUY("Bananas")
API.PORTIONS:PLAYEROWNS("Bananas")

output:

BUYING Bananas
PLAYER OWNS Bananas

1 Like

thank you so much for this
sorry @HugeCoolboy2007 for changing the solution, I apologise

all of you who tried to help me know that, I appreciate it, thank you so much for your time

2 Likes

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