I was wondering, how can I split the functions in my ModuleScript into multiple ModuleScripts when using OOP with it?
Let’s say this is my script:
local Car = {}
Car.__index = Car
function Car.new(Key, Speed)
local newCar = {}
setmetatable(newCar , Car)
newCar.Key = Key
newCar.Speed = Speed
return newCar
end
function Car:SetSpeed(newSpeed)
self.Speed = newSpeed
end
return Car
How could I split the “SetSpeed” function into another ModuleScript?
You will first need to send through the car info, then the ModuleScript has to add a function :SetSpeed() into the car, then you have to use the :SetSpeed() function.
I didn’t understand it either, but I found a work around. Since what I’m currently working on has multiple “APIs”, and easy way to get a new set of functions would just to make it be like;
return newCar and {
EngineAPI = require(script.EngineAPI)(Key, Speed)
}
and then I have a return function in the ModuleScript which returns a table of functions, like this:
return function(key, speed)
local EngineAPI = {}
function EngineAPI:SetSpeed(newSpeed)
speed = newSpeed
end
return EngineAPI
end
I meant it like so: you have a function inside the module script, this function needs the car array parameter, then you add the :SetSpeed function to the car array.