How to get a OOP object?

How can i find OOP Object

For example:

local Lamp = {}

Lamp.__Index = Lamp

function  Lamp.new()
	local Lamp2 = {
		
	}

	setmetatable(Lamp2, Lamp)
	
	
	return Lamp2
end

So if would go to a different script an create an OOP object whit Lamp.new how would i find this newly created Object.

Since Lamp.new() returns the created object, you can set a variable equal to Lamp.new(), which will create the object and set it the variable you specify.

Example of another script creating an object

local Lamp = require(game.ServerStorage.Lamp) -- insert the location of the module script inside the require

local newLampObject = Lamp.new() -- creates the new object, and set its to the varaible "newLampObject"

-- now you can access the object using the "newLampObject variable

Hope this helps!

1 Like

You can place your lamp code in a ModuleScript and create the object in a script.

ModuleScript

local lamp = {}
lamp.__index = lamp

function lamp.new()
	local self = setmetatable({}, lamp)
	
	self.enabled = false
	
	return lamp
end

function lamp:enable()
	print("Turning lamp on")
end

function lamp:disable()
	print("Turning lamp off")
end

return lamp

Script:

local Lamp = require(script.Parent.Lamp)

local newLamp = Lamp.new() -- Creates a new lamp object
newLamp:enable() -- Calls the enable function in the lamp object
newLamp:disable() -- Calls the disable function in the lamp object

image

1 Like

I already found a solution. But thanks anyway