Attempt to call nil value on "self"?

I was trying to make a part clone by using OOP, while trying to get it I came across an error. I think “self” is nil. Here is the script:

local Object = {}
Object.__index = Object

function Object.new(position,part)
	local newobject = {}
	setmetatable(newobject,Object) 
	
	newobject.Position = position
	newobject.Part = part
	
	return newobject
end

function Object:Spawn()
	local clone = self:Clone()--Error lies here, "attempt to call a nil value"
	clone.Parent = game.Workspace
end

return Object

Main script:

Object = require(game.ServerScriptService.ModuleScript)
local part = game.ServerStorage:FindFirstChild("Part")

newobject = Object.new(Vector3.new(0,0,0),part)
newobject:Spawn()
2 Likes

You’re trying to call clone on a table? It should be
self.Part:Clone()

2 Likes

Ill look into it and get back to you soon.

Are you trying to clone the part in that OOP? if so, self.Part:Clone() as @Pokemoncraft5290 said before me haha. You can’t clone self

2 Likes

It works now. Thank you. But why do I have to include, self.Part?

Because you were tryign to use :Clone() on a table instead of the Part that you wanted to clone

1 Like

Now I understand. Thank you. Also when the part is cloned the position isn’t at 0,0,0. Any reason for that?

1 Like

Probably because you stored the position in the table yet you never use it, I think you wanted to do

function Object:Spawn()
	local clone = self.Parent:Clone()
	clone.Parent = workspace
	clone.Position = self.Position
end

For the Spawn function to spawn the Part clone at the position in the Object

1 Like

I understand oop more now. Thanks for your help.

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

Also I recommend you mark @Pokemoncraft5290’s first reply to your post as the solution as he was the first one to answer the original problem in your post and rightfully deserves it