Are functions included in a local table also set as local?

Might be a pointless and obvious question, need to know anyway for the sake of knowledge and avoiding globals where not required.

I hope we know what local variables are. I don’t want to waste your time with an explanation.

local StarterClient = {}

function StarterClient.new()
end

return StarterClient.new()

The table is a local variable in this script. Does that mean that the method new also becomes local, or is it a global function?

The function becomes a member of the table. So whenever you have a reference to that table, you can also access the function.

Yes, but does the method new inherit the table’s state of being a local variable or is it a global?

Obviously this is invalid syntax:

local function StarterClient.new()

So I’m wondering if new also becomes local by right of being a member of a local table.

Not quite sure what you’re asking here. Your sample code looks to be making the function and then immediately returning the function’s result.

Since it’s a member of the table, it technically inherits the table’s state. Since it can only be called via the table, wherever the table has a reference.

This is what I was asking and this answers it. Thank you.

1 Like

Neither the table nor the function have a “state” of being “local” or “global”.

The table has a local reference on the stack, the function has a reference in the table. Locality isn’t tag-based but context-based instead, so the question doesn’t actually have an answer.

Calling the function from within the table exhibits the same behavior as calling the function as a global. The only difference with the global is that you access an invisible (but present) table.

2 Likes

The marked answer isn’t really a complete answer, since it’s not “technically inheriting the table’s state.”

As you know, variables can either be local or global. Variables are a reference to a value, object, or whatever you want to call them. You can do either of these:

local localVar1 = StarterClient.new -- this is a local reference to the function
globalVar1= StarterClient.new -- this is a global reference to the same function
print(localVar1 == globalVar1) --> `true`, it's the same function.

You can do the same with a table:

local localVar2 = StarterClient -- this is a local reference to the table
globalVar2 = StarterClient -- this is a global reference to the same table
print(localVar2 == globalVar2) --> `true`, it's the same table.

In both cases, the same object has a reference as both a global and a local variable. It can’t be both local and global at the same time, can it? This is because the reference is what can be classified as either global or local, rather than the table/function itself. The StarterClient.new function is a member of the table, like @Usering said. That means you don’t have a direct global or local reference to it. That means you can’t really describe it as being either a local or global variable, since it’s not really either.

I was just trying to answer his question in the terms he asked about. Thanks for giving a more detailed answer :slight_smile:

1 Like

@posatta Is there any actual difference between assigning new to a global or local variable then? Typically I’ve been accustomed to knowing that local variables are better than globals (I never use globals), but this just offsets what I know completely. How is it “neither”? I’m not quite sure I follow.


@AMD_chan What exactly is the difference between tag and context basis? Are you referring to the attachment of local before a variable and the thread’s context, or something else?

At the very least, I understand this:

Which is a little offsetting, but I don’t think it should have any impact (right?) on anything here.

As for the last sentence, I don’t quite follow.

When you call print(), you’re actually doing _ENV.print(), where _ENV is the Roblox environment in this case.

Locality is based on context because your local table variable can be global in a different script. Context as in the context of the code, not the Roblox level thing.

So what ends up happening is while your table is on the stack, the function you’re accessing from it is not, and it will do a lookup every time. This is how both tables and the environment (a table) behave.

3 Likes