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
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.
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()
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)
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
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"