Attempt to index nil with 'Name' | OOP

Hi, I need some help on my code, I am making inventory system, using object oriented programming.

Please read all! :stuck_out_tongue:

local function RefreshInventory(InventoryTable)
	for i,v in pairs(script.Parent.Inventory.NoFreeSlots:GetChildren()) do
		v:Destroy()
	end
	for i,v in pairs(InventoryTable) do
		local Tmp = v.ViewportFrame
		Tmp.Name = v.name
		Tmp.Parent = script.Parent.Inventory.NoFreeSlots
		Tmp.Position = v.Slot.Position
	end
end

Events.Receive.OnClientEvent:Connect(function(Name)
	Inventory:AddItem(Name)
	wait(.2)
	RefreshInventory(Inventory:GetInventory())
end)

For some reason
InventoryTable is nil, no Cue why

function Inventory.Start(Player)
	local self = {}
	self.Inventory = {}
	self.Mouse = Player:GetMouse()
	self.Player = Player
	self.Holding = false
	self.Slots = Player.PlayerGui.Game.Inventory.Slots
	self.SlotsInUse = {}
	self.Current = nil
	for i,v in pairs(self. Slots:GetChildren()) do
		self.SlotsInUse[v] = false
	end
	return setmetatable(self,Inventory)
end

This is how I create it.
.__index is defined!

The trouble is here:

function Inventory:Viewports(Name,Distance)

		local object
		object = game.ReplicatedStorage.Modules:FindFirstChild(Name)
		if not object then
			return;
		end
		local ViewportTemplate = script.Viewport:Clone()
		local Camara = Instance.new('Camera',ViewportTemplate)
		ViewportTemplate.CurrentCamara = Camara
		object = object:Clone()
		object.Anchored = true
		object.Parent = ViewportTemplate
	Camara.CFrame = CFrame.new(object.Position + Vector3.new(0,2,10),object.Position)
	print(ViewportTemplate.Name)
	return ViewportTemplate
	end

So basically I do this:

table.insert(self.Inventory, {Index = 1,Slot=self:FindSlot(),ViewportFrame=self:CreateViewportFrame(Name,2),name=Name})

But… when I call from client

function Inventory:GetInventory()

return self.Inventory

end

It returns nil
An idea why this doesn’t works?
:FindSlot() works fine
And CreateViewport too.
But it seems i can’t return table to client!

The code you’ve shown thus far looks fine, however you’ve left out your Inventory:CreateViewportFrame function which could be beneficial in this scenario - or is this function the same as Inventory:Viewports?

It seems you are creating all this data on the client - are you trying to reference it on the server at all? That would result in a nil value being thrown.

Yes, :CreateViewportFrame()
Calls Inventory:Viewports()


function Inventory:CreateViewportFrame(name,Dis)
	local Viewport = self:Viewports(name,Dis)
self.Current = Viewport
	return Viewport
end

All it’s made on client!
Also, I just tested and it find the slot!
I think somehow is not creating the viewport, I mean cloning it

Can you provide the Viewports method as well.

Giving only half the code really doesn’t help. We need to trace it back to creating and selecting the viewportframe.

1 Like

Sorry, I just realized the trouble.


I placed ‘Modules’ exchange of Models

No worries - but that’s precisely the point. The issue was in a piece of code that you never even shared here.

Glad you found the issue and improved the debugging skill of tracing it back.

2 Likes