How to make functions (that uses colon ":")

Hi! I want to know how to make a default function for instances like part:Destroy() or instance:FindFirstChild("SomethingElse"). How do i do it? Thanks! (I know you read this please help me I’ve been working on it for a few weeks already)

2 Likes

It’s used in Object-Oriented Programming (OOP). When you create a “object” (commonly by using ModuleScripts), you can assign it functions that use colons.

local myClass = {}
myClass.__index = myClass

function myClass.new(args)
    local self = setmetatable({}, myClass)
    self.pie = 3.14
    self.otherProperties = args
    
    return self
end

function myClass:IncrementPi()
    self.pie += 3.14
end

return myClass

A noteworthy difference between using a colon vs. a dot operator when declaring the function is that functions with a colon will pass self automatically.

local example = myClass.new()
print(example.pie) -- 3.14
example:IncrementPie()
print(example.pie) -- now it's 6.28

Short lesson.

3 Likes

aka pi and tau
two cool numbers

1 Like

I don’t get all of it but the main reason I want it is to shorten some functions for instances like FFC, FFA, etc. Is there a way to do that?

If you’re new to OOP and don’t understand __index or setmetatable() just do:

Log = {
	logger = function(self, v)
		print(v)
	end
}

Log:logger("hey")

It’s documented here Programming in Lua : 16

1 Like

I never heard of OOP and _index ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎

1 Like

Lucky for you, I made this resource! (Today, mind you)

Stuff like this isn’t supported by default in Roblox but I made it work with metatables like they were mentioning but made it as beginner friendly as I could.

-- Set up my custom module
-- You will need to put this require code above all of your scripts, but that's the only drawback for me

local ezWrapper = ... --> set the path here
ezWrapper:Setup();

-- Add your custom function
-- It adds a function 'test' to all objects that are in the script

ezWrapper:AddBaseFunction("test",function(object,state)
	print(object,state)
    return "read you, loud and clear"
end)

-- Now you can do what you want

local response = part:test(true) --> "read you, loud and clear"

Is this the kind of thing you were looking for?

1 Like

Pretty similar, I was wanting to shorten the :FindFirstChild() function and some others by making another function that works the same but the name is FFC() to make it shorter. Is there anyway to do that?

t:f() is syntax sugar for t.f(self)

That means this

local t = {}
t.Name = "foo"

function t:sayHi()
  print(self.Name)
end

is equivelent to…

local t = {}
t.Name = "foo"

function t.sayHi(self)
  print(self.Name)
end

Both work interchangeably, in fact, you can call every single Roblox method as such: workspace.Destroy(workspace)

(You really shouldn’t though, unless feeding builtin functions like pcall)

4 Likes

Something like this should work:

local ezWrapper = ... --> set the path here
ezWrapper:Setup();

ezWrapper:AddBaseFunction("FFC",function(object,name)
    return object:FindFirstChild(name)
end)

print(part:FFC("hi"))

Please note, for all scripts that would use your :FFC method you will need to put that code at the top.

thanks! By the way what do i replace the

local ezWrapper = ...

with?

A reference to their ezWrapper module
ie: require(ReplicatedStorage.ezWrapper)

1 Like

So, get the module from this: EzWrapper - Roblox

And then place it in somewhere like replicated storage and then you can do:

local replicatedStorage = game:GetService("ReplicatedStorage")
local ezWrapper = require(replicatedStorage:WaitForChild("EzWrapper"))

Please note, for all scripts that would use your :FFC method you will need to put that code at the top.

Ok thx! ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎

If you want to shorten those the best way (in my opinion) would just be to create some regular functions.

Example:

local function ffc(instance, name)
    return instance:FindFirstChild(name)
end

local Workspace = game:GetService("Workspace")
print(ffc(Workspace, "Terrain"))

(I’d recommend not doing this and using the built-in functions to make your code more readable.)

Yep! I’d forgotten where that function was stored. I had previously thought it was a static function of the Instance class but couldn’t find it there. WorldModel is a pretty good place for it. Thanks for letting me know :happy2:

If you care about efficiency, using a function is the smartest way to do this. Wrapping objects is bad for performance. Number of arguments also doesn’t really matter. (Optimizing FindFirstChild also doesn’t really matter, it’s really not a performance bottle neck). Doing it this way is the same number of characters

Workspace:ffc("Terrain") -- 24

compared to:

ffc(Workspace,"Terrain") -- 24

not to mention more efficient. In the end though, you should probably go for readability. If you have problems typing I’d highly recommend taking a typing course online, they’re pretty helpful.

In this case, you could just do:

local ffc = game.FindFirstChild

local terrain = ffc(workspace, "Terrain")
1 Like

Dude ofc ik that i can also just make it a function but it’s not using colon it uses arguments. Anyone can make that and I don’t even need to make a post on it. But what I need is using a colon to reduce the amount of arguments required. It’s always about efficiency.

Read these informative resources -
What is self and how can I use it? - Help and Feedback / Scripting Support - DevForum | Roblox
Programming in Lua : 16

Both are gonna be helpful and easy to understand I believe.