How can I split ModuleScript functions into multiple ModuleScripts when using OOP?

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 don’t get what you exactly mean, do you want to create another class and inherit the functions from this?

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

Not the best way to handle this, but it works.

I still didn’t understand but good you solved it.

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.