How do i return this index key to getcharacterasync

Whenever i do this, it keeps erroring. but when i do math.random(key) it doesnt error

any help on this?

local module = {}
module.__index  = module

module.Mt = setmetatable({}, {
	__index = function(self, key)
		if type(key) ~= "number" then 
			return rawget(self, key)
		end
	    
		return game:GetService("Players"):GetCharacterAppearanceAsync(key)
	
	end,
})


return module
	

What error does it give you?

If key is nil, then math.random(key) will work as you can call random without any arguments.

13:34:24.245 - attempt to yield across metamethod/C-call boundary

Methods suffixed with Async are yielding calls, that is why this error occurs. Unfortunately, you need to use a method for this.

local appearances = {}
local module = {}

function module:GetCharacterAppearance(key)
  if not appearances[key] then
    appearances[key] = game:GetService("Players"):GetCharacterAppearanceAsync(key)
  end
  return appearances[key]
end
3 Likes

Thought i could use an __index function, oh well. Thanks for the help!

No problem! I think this is possible in newer versions of Lua, but not in the 5.1 which Luau is based on.