Help with .new in function name and OOP

So I was reading this post: Concepts: Object Oriented Programming in Roblox
and in this code sample:

local Car = {}

function Car.new()
	local newCar = {}
	
	-- Properties
	newCar.Model = "EpicModel"
	newCar.Color = "Blue"
	newCar.Speed = 0
	
	-- States
	newCar.isDriving = false
	
	-- Behavior
	function newCar:Drive()
		self.isDriving = true
	end
	
	function newCar:Stop()
		self.Speed = 0
		self.isDriving = false
	end
	
	function newCar:SetSpeed(speed)
		if self.isDriving then
			self.Speed = speed
		end
	end
	
	return newCar
end

return Car

it has .new in the function name, what does this mean and how does the overall script work?

.new is nothing special, it is just a convention to “name” your constructor that, since well it creates a new instance of a class. However that closure approach is definitely not a good one. Say you end up with 50 car instances, that means you have 150 copies of the same 3 functions.

That is why a metatable approach would be better, since it is reusing the same functions.

local Car = {}
Car.__index = Car

function Car.new()
	local newCar = {}
	
	newCar.Model = "EpicModel"
	newCar.Color = "Blue"
	newCar.Speed = 0
	newCar.isDriving = false
	setmetatable(newCar, Car)
	
	return newCar
end

function Car:Drive()
	self.isDriving = true
end
	
function Car:Stop()
	self.Speed = 0
	self.isDriving = false
end
	
function Car:SetSpeed(speed)
	if self.isDriving then
		self.Speed = speed
	end
end

return Car
3 Likes

So if you left the .new out of the function name the code will still work?

I could do function Car.old() if i wanted to be funny or name it function Car.cmrjgntrigte() and it would still work. new doesn’t mean anything special in Lua(u), it is just a convention to name constructors that.

So when the module script is required, why does the Car.new function run? It isn’t called anywhere in the script.

Not sure what you mean—could you elaborate?

The script that’s in the code sample is a module script, I don’t see anywhere in the module script where ‘Car.new()’ is called. So how does the Car.new function actually run in the first place when the module script is required?

It won’t run when the module is required, you will have to run it yourself like this:

local carModule = require(modulePath)

local carObject = carModule.new()
--Now you can use the methods defined earlier in the module, on carObject

carObject:Drive()
2 Likes

Wouldn’t you have to do:

local carObject = carModule.Car.new()

???

You don’t want to call it from within the module itself, as that would defeat the object of modules.

You require the module into a Script or LocalScript, and then call .new() on the module. And because it’s a module, you can require it from as many scripts as you like.

Let’s say we put @sjr04’s module in ReplicatedStorage and named it “Car”.

game.ReplicatedStorage.Car:

The “Car” class is returned from the module. We can then call this directly from our script (which we’re going to call “Test” and place in ServerScriptService).

game.ServerScriptService.Test:
local Car = require(game:GetService("ReplicatedStorage").Car)

local MyCar = Car.new()

MyCar:Drive()
MyCar:SetSpeed(30)
1 Like

No, you wouldn’t need to do that as you are doing this

local Car = {}

function Car.new()
   ...
end

return Car

So basically what you have done here is set a function inside the table Car, and then you are returning it, which means when you require the module, just think of that Variable you are requiring it with as of the Car table and call the function .new() on it

Please correct me if I am wrong.

What do you mean, the function is outside of the table as seen in the code or is that what the .new does? Put it inside of the table? Which is probably wrong lol. I’m so confused

Ok, so seems like you aren’t understanding it, but what you are basically doing here:

local Car = {} --This Is Your Table

function Car.new() --You did Car.new, meaning it creates a function called new, and inserts it into the table called Car.

end

return Car --Return the table, so anyone requiring the module can use the functions inside the table "Car"
1 Like

Thank you so much! This clears up my confusion. :+1: :+1: :grinning: :grinning:I thought Car.new was a completely different name and had nothing to do with the table…

Oh, its just like assigning properties to a table I believe. for example:

Car.Name = "BlaBla"

except what I showed above was a function, not just a string.

And like @sjr04 said that .new doesn’t matter, its just a name for the function and is a widely used convention.