How would I make a custom function?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I want to make a custom function “:GetName()”

  1. What is the issue? Include screenshots / videos if possible!

I dont know how to make a custom function :GetName()

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local workspace = workspace
workspace:GetName()

--Output "Workspace"?

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

IF ANYONE CAN HELP THAT WOULD BE GREAT

1 Like

You can write a local function like this

local function GetName(instance)
return instance.Name
end)

print(GetName(workspace))
1 Like

I know I can write one like that but I want to me able to write ones that use a colon. example

workspace:GetFullName()

My custom one

workspace:GetName()

You see?

To my knowledge you can do this in a module script

--module
local module = {}
function module:GetName(instance)
print(instance.Name)
end
return module
---module
---script
local m = require(modulescript)
m:GetName(workspace)
---script
1 Like

How do I create like Ui Libaries etc with this then?

–Module

local Object = {}
Object.__index = Object

function Object.new(Part)
   local self = setmetatable({}, Object)
   self.Object = Part
   return self
end

function Object:GetFullName()
   return self.Object:GetFullName()
end
 
return Object


–Normal script


local ObjectModule = require(game.ServerStorage.ModuleScript)

local Part = ObjectModule.new(workspace.Part)

Part:GetFullName()

This example above is pretty useless but you can do much more powerful things with it.

3 Likes

So how would I go about using that to make a Ui Library Like “Wally Hub Ui Library V2”

What is that can you explain it

1 Like

In your second script, does m = module? (aka table). Then you pass “m” as the first argument to GetName function with workspace and from that you get the instance’s name printed?

4 years later… Kinda late, aren’t you?

2 Likes

Sorry, I just wanted to clarify some stuff…

I’m not trying to be mean, I’m just saying you’re “kinda” late

1 Like

Oh it’s ok, I got your joke lol. But anyway, just wanted for the original person to explain the script or for someone else to…

1 Like

This is not a function, this is a method. Whenever you use the colon after a object you are calling a method attached to that object.

Example:

BasePart:Destroy()

Destroy is a method of basepart. To make your custom method you would need to learn object oriented programming. You create your own object like basepart and attach custom methods to thay object. There are many great tutorials and forums for this. Your not attaching the method to a pre existing object. For your solution make a custom function that simply gets the workspaces name and returns it.

Example:

local function GetWorkspaceName()
    return workspace.Name
end

<< methods are basically functions attached to objects and methods are the correct name but functions could work too.

You don’t do method newMethod() when creating a new method/function though? You instead do function newMethod(). But methods AND functions are the correct names, not just methods.

1 Like

Methods like part:destroy are technically functions. but they are only attached to that specific object You can read the descriptions it says “sets the parts parent to nil and disconnects all functions connected to that object” and methods are the correct name all because they are the same colour in default theme does not mean they are the same.

When you use the colon it automatically passes the object (part) as self you should learn about OOP it will help you get a better understanding.

Here is link where you can read about functions it explains methods at the bottom

Edit wth i didnt even realize this was four years ago i just saw it on latest

Lol yeah

1 Like