OOP Property Issue

I’m just playing around with a couple of code, but stumbled upon either a bug or just a common mistake.

local newLaser = Laser.new(workspace.Origin)
print(newLaser.Origin.Name)

newLaser takes in an origin part and sets a made up proprty, Origin, to workspace.Origin.

function laser.new(origin)
	local newLaser = {}
	setmetatable(newLaser, laser)
	
	newLaser.Origin = origin
	
	return newLaser
end

In this line, it sets the origin, etc.

The error it puts out is,

ServerScriptService.Script:6: attempt to index nil with 'Name'

I’ve looked at tutorials on how to learn OOP, but they look exactly identical. Any help and knowledge appreciated.

Try this out:

function laser.new(origin)
	local newLaser = {}
	
	newLaser.Origin = origin
	
	return setmetatable(newLaser, laser)
end

I tried this method, it worked, but in the other OOP examples it did the same method as I did.

And in other modules it seemed to have worked aswell?

That does not make any difference, I think its the argument named origin is the problem.

I don’t understand? What do you mean by argument?

Have you tried printing the result of origin, like typeof(origin), from the function laser.new(origin).

Could you show your entire module script code?

Nope, but when I used the print statement inside the function, it referenced to that specific part in workspace.

--// Variables
local laser = {}
laser.__index = laser

local visibleKey = "Visible"

local DIST = 100

--// Functions
function laser.new(origin)
	local newLaser = {}
	
	newLaser.Origin = origin
	
	return setmetatable(newLaser, laser)
end

function laser:Activate()
	local function createBeam(origin)
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		
		local raycastResult = workspace:Raycast(origin.Position, origin.CFrame.LookVector * DIST, raycastParams)
		
		if raycastResult then
			return raycastResult
		end
	end
	
	self.CurrentOrigin = self.Origin
	print(self.Origin.Name)
	
	repeat wait()
		local result = createBeam(self.CurrentOrigin)
		
		if result then
			print(result.Instance.Name)
			self.Status = "Success"
		end
	until self.Status == "Success"
end

function laser.__newindex(t, k, v)
	if k == visibleKey then
		if v then
			-- We'll do something later with these.
		else
			-- We'll do something later with these.
		end
	end
end

setmetatable(laser, {
	__newindex = function(...)
		laser.__newindex(...)
	end,
})

return laser

I tested your setup, it successfully printed out the name of Origin. Is workspace.Origin getting deleted in any way?

Nope. Just checked in studio, it’s anchored.

Wait hold on. That’s the new code…

Try this out, what does it print?

function laser.new(origin)
	local newLaser = {}

	newLaser.Origin = origin
	warn(tostring(newLaser.Origin))
	warn(typeof(newLaser.Origin))
	warn(tostring(newLaser.Origin.Name))

	return setmetatable(newLaser, laser)
end

I believe your first method worked, it’s just that the original code was written in the same format as this one?

I think I might’ve fixed it… I had to delete the __index function because it was OVERRIDING the original one set!