I need help at making custom functions please

Hellk dears , I’ve always wondered how OOP functions like GetChildren & TakeDamage and things like that work . I mean , for example, we use game:GetService’Players’.LocalPlayer.Character.Humanoid:TakeDamage(1) , but i really want to know how TakeDamage really defines the last index so I’d be able to create smth like Humanoid:customfunction() if possible .

1 Like

You can’t create custom methods for Roblox’s core/own instances nor Data Models. Instead you can use ModuleScripts to create classes:

3 Likes

What about something like string.find which you can use (‘string’):find also ? Do you know how it defines the (‘string’) ?

2 Likes

just lua’s string librarys, its just how it is in lua

i think you original question was asking how you can just add methods to instances in roblox, and to that, i’m sorry to say its not possible, but you can wrap your instance in a custom class like this:

local HumanoidWrapper = {}
HumanoidWrapper.__index = HumanoidWrapper

function HumanoidWrapper:new(humanoid)
    local self = setmetatable({}, HumanoidWrapper)
    self.humanoid = humanoid
    return self
end

function HumanoidWrapper:customfunctiontogethealth()

    return self.Humanoid.Health
end

local humanoid = game:GetService("Players").LocalPlayer.Character.Humanoid
local wrappedHumanoid = HumanoidWrapper:new(humanoid)
wrappedHumanoid:customfunctiontogethealth() -- Calls custom function
2 Likes
-- Example 1: Basic usage
local s = "Hello, world!"
local pattern = "world"
local start_index, end_index = string.find(s, pattern)
print(start_index, end_index)  -- Output: 8 12
-- Example 2: Using init parameter
local s = "Hello, world! Hello, Roblox!"
local pattern = "Hello"
local start_index, end_index = string.find(s, pattern, 10)
print(start_index, end_index)  -- Output: 15 19
-- Example 3: Using plain parameter
local s = "Hello, world! (Hello)"
local pattern = "(Hello)"
local start_index, end_index = string.find(s, pattern, 1, true)
print(start_index, end_index)  -- Output: 14 20
-- Example 4: No Pattern found
local s = "Hello, world!"
local pattern = "Roblox"
local start_index, end_index = string.find(s, pattern)
print(start_index, end_index)  -- Output: nil

These are basic examples on how to use string.find
What actually do you mean by (‘string’):find?

If this is what you mean:

local pattern = "world"
local start_index, end_index = string.find("Hello, world!", pattern) -- Used string directly instead of a string variable.
print(start_index, end_index)  -- Output: 8 12

This would still work perfectly fine.

If this is what you mean:

(`string`).find(s, pattern)

It’s the same thing.

Also @krazeems’s answer is very helpful incase that was what you asked for.

2 Likes

Still cant understand __index & setmetatable. Can you teach me in a simple way ? ( i know self)

2 Likes

__index is what’s called a metamethod, it’s used in a metatable.

Metatables

A really easy way to think of them is just as a table about a table. It controls how the table behaves with certain things.

The nature of OOP includes inheritance, right? how the objects inherit methods from the classes?
Well, Lua doesn’t let us do that. We need to use metatables instead.

The __index metamethod

This is invoked when Lua can’t find something in our table. We can control a default value or a secondary table to be searched. For example:

--it's also important to note setmetatable returns the first parameter,
--so the table we are assigning a metatable to

local tbl = setmetatable({Test = 3}, {__index = function(_self, _key)
    return "default value"
end})

print(tbl.Test) --this value is found, so it would output 3
print(tbl.No) --this value does not exist, so the function is run and would output "default value"


--now, we set it to a table
--let's store the extras in another table
local otherValues = {
    Test = 1,
    Test2 = 2
}

setmetatable(tbl, {__index = otherValues})
--now, when we look for a missing value, Lua will search this other table, too
print(tbl.Test) --> 3 because tbl has a key called test. __index is not invoked.
print(tbl.Test2) --> 2, the metatable was searched
print(tbl.Test3) --> nil, neither the table or the metatable has this value, so it's nil.

The context of this in OOP

We use __index to redirect the objects to the classes when we call methods with our given parameters. It’s also important that colon notation when defining a function just passes the object itself as a parameter, so the metatable isn’t referenced at all. We only call the function.

local Class = {}
Class.__index = Class
--[[
we set the index to the table itself. This means that, since the class will
get set as the metatable to each object, __index will be invoked each time
a value is not found. We redirect Luau to this table, which holds all of
our methods.
]]

function Class:foo()
    print("This was called from the metatable.")
end

function Class.new()
    return setmetatable({}, Class) --no attributes assigned here, returning it directly
end

return Class
local Class = require(--[[to class module]])
local Object = Class.new()

Object:foo()
--1. Does it exist in the table? No. Check the metatable.
--2. Does it exist in the metatable? Yes. __index returned the method.
--3. Call the function with our parameters.

I hope this helps! Other metamethods like __call, __newindex, __tostring and many more are also extremely useful.

Metatables and Metamethods | Documentation - Roblox Creator Hub

1 Like

So metatable returns the first table we put in first arg and so we can do researches jn it and the desired thing doesn’t exist it checks the second table which is the second arg , is it the use of metatable, or i misunderstood?

yeah, basically. These three code snippets do the same thing:

local x = setmetatable({}, {__index = function() --[[...]] end})
local x = {}
setmetatable(x, {__index = function() --[[...]] end})
local x = {}
local meta = {}
meta.__index = meta

setmetatable(x, meta)



This isn’t so much the entire use of the metatable, this is just one metamethod. __index only gets invoked when the search value is nil in the first table.

Like I said before, other metamethods exist to change the way the tables behave, like __call, which gets invoked when the table is called like a function, or __tostring which gets invoked when you try and cast the table to a string.

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