ROBLOX Auto-Complete in OOP Module Scripting

I recently picked up Object-Oriented Programming in ROBLOX. I have a few years of experience in coding with OOP languages (C#, Java, JavaScript etc.) but I never encountered this problem I have currently with Lua.

So long story short here’s my Person class:

local Person = {}
Person.__index = Person

function Person.new(name, age,gender)
	local self = setmetatable({}, Person)
	self.name = name
	self.age = age
	self.gender = gender
	return self
end

function Person:PrintPerson()
	print("Name:", self.name)
	print("Age:", self.age)
	print("Gender:", self.gender)
end

return Person

And the script that tests the module:

local PersonClass = require(game.ReplicatedStorage.Person)
local Gender = require(game.ReplicatedStorage.Gender)

local name = "Jefferson"
local age = 15
local gender = Gender.Male

local person = PersonClass.new(name,age,gender)

person:PrintPerson()

Now the issue is that when I try to call the PrintFunction() from the Module Script ROBLOX recommends me the : functions even through I’m using the .

image

I’ve been trying to find a way to tell the module script to only let itself be called with a : but there doesn’t seem to be any way to do it

The issue is small and not world breaking but I want to fix it to avoid future migraines of accidentally calling the function in an undesired way

2 Likes

This isn’t a bug but a Roblox feature, and you can’t fully hide those, unfortunately.

In Luau OOP, you can call these functions in two different ways:

Person:PrintPerson()

or 

Person.PrintPerson(self)
1 Like

I’m aware of it being a feature to call a function in both ways

. being without self being passed
: being self getting automatically passed

That’s why I’m trying to protect my code from accidentally calling functions that require self being called without it

Is there really no way?

The sad truth is no, there is no way to hide those.

I myself had many users constantly misuse my OOP modules because of this feature.
The only way to help users is to write a detailed read of how to use the module.

1 Like

I guess such a dynamic low level language comes at a cost then. Being careful when using the module functions and proper documentation is the solution.
Thanks a lot!

1 Like

I guess the only way you could possibly fix this is by detecting if self doesn’t exist and applying a custom warning/error to notify the user about using colon functions instead.

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