Roblox metatables do not work

  1. Im making a relatively simple ui element

  2. class “play” only holds inherited methods. If i try to call setupownconnections, it errors, saying it doesnt exist

honestly this might just be an engine issue, because there is absolutely no reason that this wouldn’t work.

View class, what play inherits from

--!strict
-- packages
local ViewType = require(game:GetService("ReplicatedStorage").Shared.Protocols:WaitForChild(`View.H`))

local template: ViewType.View = {
	
	vName = "Template",
	
	vScriptCam = true,
	
	vCamRig = nil,
	
	vOveride = true,
	
	vReference = game:GetService("ReplicatedStorage").Client.Views.Play,
	
	vNeedsSort = true,
	
	vSortType = nil,
	
	vSetOwnConnections = false
	
}
local View = {}

View.__index  = View

function View:Init()
	
	self = setmetatable({}, View)
	
	self._Values = template
	
	self.Enabled = false
	
	self.ViewType = "Template"
	
	self._Values.vReference.Enabled = false
	
	return self
end

function View:Disable(plr)
	
	self.Enabled = false
	
	local Gui = self._Values.vReference
	
	Gui.Enabled = false
	

	
	local cam = workspace.CurrentCamera
	
	cam.CameraType = Enum.CameraType.Custom
	
end
----------
----------
----------

function View:Enable(plr: Player)
	
	self.Enabled = true
	
	local Gui = self._Values.vReference
	

	
	if self._Values.vScriptCam == true then
		
		local camera: Camera? = workspace.CurrentCamera
		
		camera.CameraType = Enum.CameraType.Scriptable
		
		camera.CFrame = self._Values.vCamRig.Head.CFrame
		
	end
	

	
	
	
	-- enable gui
	
	Gui.Enabled = true
	
end







return View

play class – this is where the error resides

--!strict
local prefab = require(script.ColorSlotPrefab)

local NetWork = require(game:GetService("ReplicatedStorage").Shared.Signal.ClientSignal.Signal_Module)

local View = require(script.Parent)

local ViewType = require(game:GetService("ReplicatedStorage").Shared.Protocols:WaitForChild(`View.H`))

local PlayTemplate: ViewType.View= {
	
	vName = "Play",
	
	vScriptCam = true,
	
	vCamRig = game:GetService("Workspace").CameraRigs.PlayRig,
	
	vEnableOnStart = true,
	
	vOveride = true,
	
	vRegularView = false,
	
	vReference = game:GetService("ReplicatedStorage").Client.Views.Play,
	
	vNeedsSort = false,
	
	vSortType = nil,
	
	vSetOwnConnections = true
}

-- Util Functions

local function ButtonSetup(self) : {RBXScriptConnection}
	local returnables: {RBXScriptConnection} = {}
	for i, v in pairs(self.RecognizedColors) do
		if self.Buttons[i] ~= nil then self.Buttons[i]:REMOVE() end
		
		self.Buttons[i] = prefab:New(self._Values.vReference.SkingColorSelector.ColorHolder)
		
		local NewCon =self.Buttons[i].SlotPrefab.Activated:Connect(function(etc)
			NetWork.FireSer(false, "ChangeColor", v)
		end)
		table.insert(returnables, NewCon)
	end
	return returnables
end

local function SetUpOtherConnections(self)
	
end


local Play = {}
Play.__index = View:Init()

function Play:SetOwnConnections() : {RBXScriptConnection}
	local ConnectionTable = {}
	local waste = ButtonSetup(self)
	print("Nice")
	for i,v in pairs(waste) do
		table.insert(ConnectionTable, v)
	end




	return ConnectionTable
end


function Play:Init(plr: Player)
	
	
	self = setmetatable({}, Play)
	
	self._Values = PlayTemplate
	
	self._Values.vReference.Parent = plr.PlayerGui.CurrentView
	
	self._Values.vReference.Enabled = false
	
	local colors: {[string]:Color3} = {
		["Black"]= Color3.new(0, 0, 0),
		["Red"]= Color3.new(0.333333, 0, 0),
		["Tan"]= Color3.new(1, 1, 0.498039),
		["DarkTan"]= Color3.new(0.368627, 0.368627, 0.184314),
		["Brown"]= Color3.new(0.666667, 0.333333, 0)
	}
	self.RecognizedColors = colors
	
	self.Buttons = {}
	
	self.Enabled = true
	
	self:Enable(plr)
	
	
	
	
	return self
end



return Play


Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

This is why you don’t use OOP or metatables in the first place

But to answer your question…


The reason it errors is because you don’t actually add Play itself to the __index

Fix:

local Play = {}
setmetatable(Play, View:Init())
Play.__index = Play

This should fix your issue.


I also don’t think this is a good idea…