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:


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


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:

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.

1 Like

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