Is it possible to make __index redirect to a value instead of a table?

Let’s keep it simple, I want to make __index redirect to a value instead of a table, is it possible?

Code for better understanding:

ModuleScript:

module.Player = Players.LocalPlayer
module.__index = module.Player
module.Char = nil

LocalScript:

local Player = require(script.Players)
-- Here I want to return a Player instance,
-- but still be able to call Player.Char to get it's own Character variable.
local Character = Player.Char

This gives a fairly good insight into how you can use the _index metamethod:
https://www.lua.org/pil/13.4.1.html

Yes, you can just set the _index metamethod to a value. This will just make it so that if something is called which it can’t find in the main table it will default to that value.

2 Likes

Thanks, didn’t know you could set index to a function.