Is there a way to get the full path of a nested index in a metatable?

I want to make it so that if I index Example.Settings.Volume on a metatable, I can like get the full path?

I’m planning to make a system where if you index a table like shown above, the server can return a custom value (or return with the value from that index on the server-side?)

Metatables by default, when you index, you often have to use a proxy table because if I tried to access Example.Settings.Volume it would oly see Example.Settings, and if I tried to use a proxy metatable, it would never know until it finished indexing.

Any help?

Who are you hiding your table from? Yourself? Is that the Minecraft hidden base in singleplayer syndrome creeping into Roblox code? :joy:

Just… don’t. You can do it via recursion or a loop, but why would you want to?


local Player = {Apple = true}
Player.__index = Player
local Admin = setmetatable({__index = Player},Player)
local SuperAdmin = setmetatable({__index = Admin},Admin)

local function create_class_super_admin():()
	local self = {}
	
	
	return setmetatable(self,SuperAdmin)
end


local class = create_class_super_admin()
print(class.Apple)

See? class.Apple behind the scenes does ~4 lookups on the VM level. That’s essentially x4 slower than direct access.

You really don’t want that; you want full access.

Hence, paradigms like ECS are superior. Look at Entity Component System implementations in Roblox:

local Players = game:GetService("Players")

local Plr_Health_Component:{[Player]:number} = {}

Players.PlayerAdded:Connect(function(Plr):()
	Plr_Health_Component[Plr] = 100
	Plr.CharacterAdded:Connect(function():()
		Plr_Health_Component[Plr] = 100
	end)
	Plr.CharacterRemoving:Connect(function():()
		Plr_Health_Component[Plr] = 0
	end)
end)
Players.PlayerRemoving:Connect(function(Plr):()
	Plr_Health_Component[Plr] = nil
end)

Back to your “full path” thing… dead simple:

local class = create_class_super_admin()

print(getmetatable(class))

The real solution? Build your system without metatables. No lag, no hidden overhead, pure logic you can actually see.

Just don’t be scared of yourself - stop hiding logic from yourself.

2 Likes

You can do so with a custom __index metamethod i.e

Settings.__index = function(self, index)
    return otherTable[index] or Settings[index]
end

I’m tryna hide the table from hackers :grimacing:
I’ll try looking into ECS. Thanks though

(I severely overcomplicated something by accident it seems)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.