Table Index [ASK]

Can we index table or metatable without have to like table.abc, table.bcd
but is more like just .abc, .bcd

That’s not possible if I understand your question, and this is also a syntactic error as well. The dot operator needs two operands (the item, and the value to index out of the item)

If for some reason you want to be able to access the array components without indexing the array, you could add those key-value pairs to the global scope of the entire script, but this doesn’t really seem useful.

local function addDictionaryToGlobalScope(dictionary)
local env = getfenv()

for key, value in pairs(dictionary) do
env[key] = value
end

setfenv(1, env)
end

local stuff = {a = 45, b = 45, c = 45}
addDictionaryToGlobalScope(stuff)

print(a)
1 Like

Can this work with module too?

Yeah the same principles should apply

Can you know how to make it like modules, so other module can also print the table?

Let’s say I have a table like this in the other script

local Table = {
		embed = "A",
		message = "b",
		webhook = "C",
}

-- Use your function
local function addDictionaryToGlobalScope(dictionary)
local env = getfenv()

for key, value in pairs(dictionary) do
env[key] = value
end

setfenv(1, env)
end

addDictionaryToGlobalScope(Table)

then how to make on other script we can access that table?

What exactly are you trying to do that would require this sort of environment manipulation?

Because the one I know we can use like
If in module we have
function module.new()
setmetatable self, module

we can make something like
function module:abc()
function module:bcd()

So in the normal script it can be like
local mod = module.new()
:abc()
:bcd()

but I want to make like js
well js use
new module()
.abc()
.bcd()

And I want mine to be like
local mod = module.new()
.abc()
.bcd()

but what exactly are you making that requires this? there is probably a better way

1 Like

I’m not even sure if there is a way to directly achieve this behavior in Roblox Studio, not that I would even recommend that you try doing anything like this.

As far as I know, there is no way to directly modify the environment of one script outside of that script.

You could use _G, or the global environment, but that comes with a lot of issues, and would require you to index everything you want to use as _G.x.

You could use BindableEvents and fire it whenever you want to add new variables to the environment for every script, but modifying a variable in one script would also require you to fire off an event, since it does not replicate to other environments.

I recommend that you just give up on this idea and become accustomed to how Roblox handles modules and variables.

Ah so I suppose I can’t make something like .abc() , so I need to use like :abc() ?

Neither would be possible when using any of the previously mentioned methods.