What does .new do in custom functions?

function inventory.new(player)

What are the uses of .new? I know Instance.new of course but I’ve rarely seen this attached to the end of function names

1 Like

I’m not really sure the best way to explain it, so try checking this post out if you want more information in detail.

You would put the name of the array first and then .new will be attached to it so it creates a new object, this is usually used with metatables or in OOP. It’s known as the constructor.

This is a basic example which sets a metatable (kinda like a table inside of a table) inside of the table so the table keeps the information of the pet.

local pets = {}

function pet.new(name, Type)
     local newPet = {}
     setmetatable(newPet, pets)
     newPet.name = name
     newPet.Type = Type

     return newPet

Then to run it you would do this to access it.

local pet = pet.new() -- returns the metatable

It depends on what that function do. They could be constructors.
Since the function name is ‘inventory.new’,
I’m assuming that it is connected to an array called ‘inventory’.

It’s usually used to create a metatable where data will be stored.

An example of this using your inventory:

local Inventory = {}
Inventory.__index = Inventory

function Inventory.new(player)
local inventoryMeta = setmetatable({Inventory = {}, Hotbar = {}, Player = player}, Inventory)
return inventoryMeta

return Inventory
local module = {}

function module.new(Arg1,Arg2)
    local Table = {}
    setmetatable(Table, module)

    Table.Arg1 = Arg1
    Table.Arg2 = Arg2

    return Table

function module:PrintArg1()

function module:PrintArg2()

return module
local a = require(script.Module).new("We","Ew")

1 Like

.new doesn’t necessarily do anything. When you use OOP(Object Oriented Programming), you create simple, lets say description of an object. This might include functions. You Create table where you “describe” this particular object and you do that with Function like this

local Vehicles = {}
Vehicles.__index = Vehicles

function car.whatever(Speed, SuspensionHeight, Model, Position)
   local Car = {}
   setmetatable(Car, Vehicles)

   Car.Speed = Speed
   Car.SuspensionHeight = SuspensionHeight
   Car.Model = Model
   Car.Position = Position
   return Car

function Vehicle:Spawn()
    Vehicles.Car.Model.PrimaryPartCFrame = CFrame.new(Car.Positoin)

now if I ever want to create a new Car all I have to do is this:

Car = Vehicles.whatever(120, 4, game.ServerStorage.Corvette, vector3.new(27, 0, 17))

As you can see .new is just name of the function. So what it does depends on what you make it to do. See THIS for great OOP Community Tutorial. I’m new to OOP myself so there might be mistakes in my code, but generally, answer to your question is that:
.new is just name of a function inside table , or metatable to be precise.

I felt like I need to say this.

As others have already said, .new is used in OOP constructor functions, however it is not some custom syntax or a required keyword. It’s simply how developers prefer to do it most of the time.
To give you the idea, it is equal to doing the following:

local inventory = {
    new = function()

Inventory is just a normal table, which in this case, can be described as an object, which is essentially a container for data, including functions

New is just a normal function inside that container

inventory = {}
inventory.new = function() end

you can include other data in the inventory object such as

other data:

inventory.ID = 1

other functions

function inventory:Destroy() end


You can assign metatables to tables for more functionality

Also something very important to note: You should only store constants or methods in the metatable connected to the object.

This isn’t much of an explanation so much as it is a code sample. A rundown of your thought process for this would be appreciated. Perhaps you have a different answer than those here who’ve already posted.

That clears up everything perfectly!

That .new there is known as a Consructor Function, which is used to make objects out of classes. You might’ve saw this in Vector3.new or Instance.new. That’s because Instance and Vector3 are actually both classes, and whenever you create a vector 3 for example, that’s an objects. And this stuff has to do with OOP, which stands for Object-Oriented Programming and it is pretty cool. You can check it out here which I wrote myself but it kind of sucks, but there are many other articles out there!

I’m gonna over what classes and objects are but briefly.


Most tutorials describe classes as a blueprint, a blueprint that contains properties and methods (aka functions). For example there, I’m sure you once worked with the ClassName property, roblox is filled with classes, like the Part class, which of course will contain as said properties and methods.

You can see properties such as “BrickColor” and “Transparency”, and methods such as “GetChildren()” or “Destroy()”. So out of these classes we can construct objects out of these classes!


Objects are constructed out of o whatever class you had, let’s say you had a class for a car for example, we take the properties and methods from the car class, and make a “Car” object from that class.

Here is an example, we use the constructor function to create an object out of this class. We have properties and methods we can see and interact with too!

And yeah, this is a script showing how to make your own constructor function and classes and objects.

Car = {}
Car.__index = Car

function Car.new(speed, colour, driver)  
   local newCar = {}
   setmetatable(newCar, Car) 

   newCar.speed = speed or 0
   newCar.colour = colour or "Grey"
   newCar.driver = driver or "No one"

   return newCar

function Car:Boost(increment) 
   self.speed = self.speed + increment

local Car1 = Car.new(50, "Blue", "StarmaQ")
Car1.colour = "Applesauce"
print(Car1.colour, Car1.driver)


OOP in roblox uses a lot of special syntax, we also need to use metatables and all that.
I won’t really go over this sorry, but here is a really good explanation on it.


Oh and also! .new is just a common name for constructors, you can name them whatever you want, another examples would be .Angles for example or .fromRGB for example.
These are all used to create objects out of classes, CFrame and Vector3 and Color3 and many others are classes