Invalid argument #2 to 'setmetatable' (nil or table expected, got Instance)

Hello I ran into this problem, Im not sure what to do. Here is the code:

local Door = {}

Door.__index = Door

function Door:New(model)

	local newDoor = setmetatable({}, self)
	newDoor.__index = newDoor
	newDoor.Model = model
	
	return newDoor
	
end

Error: ServerScriptService.Door:13: invalid argument #2 to ‘setmetatable’ (nil or table expected, got Instance)

1 Like

self is the issue. Put in Door instead of self.

How are you calling this function?

2 Likes

It sounds like he’s doing Door.New(model)

1 Like
local collectionService = game:GetService("CollectionService")
local Doors = collectionService:GetTagged("Door")
local Door = require(script.Parent.Door)

for i, door in pairs(Doors) do
	
	local newDoor = Door.New(door)
	newDoor:ToggleState()
	
	wait(4)
	
	newDoor:ToggleState()
	
end

I get this error: ServerScriptService.DoorHandler:8: attempt to call a nil value. Door handler is the script above me! Where the function is being called, also here is the full module script.

local tweenService = game:GetService("TweenService")


local Door = {}

Door.__index = Door

Door.Closed = false
Door.ClosedTime = 1

function Door:New(model)

	local newDoor = setmetatable({}, self)
	newDoor.__index = newDoor
	newDoor.Model = model
	
	return newDoor
	
end

function Door:CreateTween(properties)
	
	local Tween = tweenService:Create(
		self.Model.Door,
		TweenInfo.new(self.CloseTime, Enum.EasingStyle.Sine, Enum.EasingDirection.In),
		properties
	)
	
	return Tween
	
end

function Door:TuggleState()
	
	self.Closed = not self.Closed
	
	local properties = {}
	properties[true] = {self.Module.ReferencePoints.Closed.Position}
	properties[false] = {self.Module.ReferencePoints.Opened.Position}
	
	local Tween = self:CreateTween(properties[self.Closed])
	Tween:Play()
	
end



return Door

Do

local newDoor = Door:New(door)

instead of

local newDoor = Door.New(door)
2 Likes

I still get the same error, Something is wrong with:

newDoor:ToggleState()

In your module, it shows

function Door:TuggleState()

so use:

newDoor:TuggleState()
3 Likes

ServerScriptService.DoorHandler:8: attempt to call a nil value

local collectionService = game:GetService("CollectionService")
local Doors = collectionService:GetTagged("Door")
local Door = require(script.Parent.Door)

for i, door in pairs(Doors) do
	
	local newDoor = Door:New(door)
	newDoor:ToggleState()
	
	wait(4)
	
	newDoor:ToggleState()
	
end

Use newDoor:TuggleState() like I said. You still haven’t changed it.

1 Like

thanks for your help!! its all working now