Condensing Reused Scripts

I am currently using a Script in a Model as well as similar Scripts in other Models with only a few changes in variables. How could I condense these Scripts into a single Script with the variables as input parameters? Would ModuleScripts work?

Thanks :slight_smile:

Module Scripts are like CSS in HTML programming; thats to say that you basically outsource the code to another script. So when I put Variable.FunctionName() I use all the code inside of that function in the Module Script to the script I am using now. That parentheses is used to input variables like โ€œcharโ€ and โ€œplayerโ€, etc. I hope this made sense to you.

If I were to define a variable as an instance, but then place the code inside of a ModuleScript function, it gives errors. How would I properly do this?

For example, this might apply if my code contained this and was moved to the ModuleScript:

local Wheel = script.Parent.Car.Wheel

Yes.
You can have the script be a dummy that simply has the module do all the work. Something like this.

-- ModuleScript
local Module = {}

function Module.Initialize(source: Script, data: any)
	local Wheel = source.Parent.Car.Wheel
	-- Put the generic script body here
end
-- Script
local Module = require(PATH_TO_MODULE)

Module.Initialize(script, someData)

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