Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Error: Expected ‘:’ not ‘.’ calling member function (?)

I was just using metatables and instances to add custom properties and methods. I put the __index of the metatable to the instance that I'm trying to add extra properties to. But when I try to call a method such as customClass:FindFirstChild("SomeObjectName") I get this error:

Expected ':' not '.' calling member function FindFirstChild

(And yes, I called the function with a :)

My understanding is that there is something wrong with Indexing in lua so when it doesnt find the function insied the metatable it looks through the __index value and finds a function, and it calls it with the . instead of :.

I also tried doing customClass.FindFirstChild(customClass, "SomeObject") and the same error occured.

How would I fix this without adding all the indexes in the metatable then calling the functions in there?

Example of what I want to avoid having to do:

local _instance = Instance.new("Part")
local customClass = {}
customClass.__index = _instance

function customClass.FindFirstChild(name)
  return _instance:FindFirstChild(name)
end

Any help is appreciated! If you need more information, just ask!

1 answer

Not sure what you're doing wrong but I've tried your code, and it seems to be working correctly

Code:

local _instance = Instance.new("Part")

--// insert a tab into our part for our new function to find! :)
local _tab = Instance.new("BoolValue",_instance)
_tab.Name = "Blah"
local customClass = {}
customClass.__index = _instance

function customClass.FindFirstChild(name)
	return _instance:FindFirstChild(name)
end

local Chk = customClass.FindFirstChild("Blah")
print(Chk,typeof(Chk),type(Chk), Chk.Parent) -- prints(Blah Instance userdata Part)

This discussion is preserved as it was. New answers, comments, and votes are closed.