-- in module
local BasicSound = {}
BasicSound .__index = function(t,i)
if BasicSound [i] then return BasicSound[i] end --if BasicSound has required index, return its
return t.object[i] --if it doesn't have, return actual object's
end
function module:Pause()
self.Pause = true
end
function module.new(SoundObject:Sound)
local Sound = setmetatable({Volume = 15, Echo = true},BasicSound)
Sound.Object = SoundObject
return Sound
end
--in script
local Sound = module.new(workspace.Sound)
Sound.Name = "Sound1" --> Perfect
Sound:Pause() -->Perfect
Sound:Play() --> Expected ':' not '.' calling member function Play
I canât know how to solve this
Could you help me?
I am not entirely sure if you can do that. When you call a function using :, youâre basically passing over the index of the metatable you declared earlier. Then the __index method searches through BasicSound it seems for the function (as with regular .__index = BasicSound behavior. The issue Iâm seeing here is that the sound instanceâs methods dont have the same index or behavior as your regular functions, since their functions are called with the sound instanceâs index and not your âBasicSoundâ index. If you want this to work youâd have to somehow compare the names of the function called and search for it in the sound methods, or just have a function called :Play() that just calls the sound :Play method.
In order to use table:thing() you must use __index but have __index return a function. Iâm bad at object oriented programming, but hereâs what you would use to make this call:
local tabe = {}
local playFunction=function()
warn(âGot calledâ)
â(do your stuff here)
end
local meta = setmetatable(tabe,{
__index=function(tab,ind)
if ind==âPlayâ then
return playFunction
end
end
})
tabe:Play()
Hereâs why you have to do that, too: When you call a function from within a table, the first thing that table:action() does is find that function in the table. Then, once it has the function, it attempts to call it, hence, why returning a function works, and why you get âExpected â:â not â.â calling member function Playâ when using returning a simple value rather than a function.
if these were plain old non meta tables, you would essentially be doing this:
Your previous code: a={test=1} a.test() â error
This: a={test=function() return 1 end} a.test() â returns 1
local object = {}
local meta = {}
function meta.__index(_)
return function(parameter)
if object == parameter then
-- it's called with a ':'
print(':')
else
-- it's called with a '.'
print('.')
end
end
end
setmetatable(object, meta)
object:method() -- ':'
object.method(object) -- ':'
object.function()
It wonât work when the first parameter is the table itself, but OOP in Lua is very funky so it wonât cause any problems.
By the way, the code you included doesnât work because you canât call a method on an instance with the first parameter being a value that isnât the instance. You will need to make a proxy function that calls the method on the instance when itâs called. The code below will work.
local BasicSound = {}
local function proxy(object, name)
return function(_, ...)
object[name](object, ...)
end
end
BasicSound .__index = function(t,i)
if BasicSound [i] then return BasicSound[i] end --if BasicSound has required index, return its
return proxy(t.object, i) --if it doesn't have, return actual object's
end