Use strings as property

Hello, while I was scripting a bit i was wondering if it is able to get the property with a String.

for example:

local Folder = [Path]
local string = ":GetChildren"

Folder [string]
1 Like

Idk maybe try

folder[Name]() 

it might work maybe not

1 Like

this only works if I want to call a string. In my case it isnt a string

What are you trying to do then?

local tables = {["key"] = 123}
local key = "key"
local value = tables.key
print(value) --123

Normally, you wouldn’t include the . or : operator to index a certain functionality from a class. The only issue is when you call it via square-brackets and using the right identifier name for it, is that it does the . operation instead of : sugar syntax.

Also do not name it string as it is already sharing with an existing Lua global and would overwrite it. Therefore try an alternative name.

To do call the function properly even with a dot operator, consider the first parameter as self and therefore:
Folder[functionIdentifier](Folder, params)

Please read my first post. there it stands clearly

Like this?

Oh im sorry this was right but the issue is that it if im using for example :Destroy or :GetChildren it doesnt work because im using the property with “:” at these. Destroy will be then .Destroy

1 Like

like its only working for propertys where you use a Dot

I noticed when i use a module script and click . I get functions in the completing thing so i thought it might work here as well

@Operatik is right if you need to use : then add itself as the first parameter in the brackets.

local instanceRef = [path]
local methodAsString = "GetChildren" --or destroy

instanceRef[methodAsString](instanceRef)
local folder = workspace.Folder
local getChildren = "GetChildren"
local children = folder[getChildren](folder)

Thanks this worked. I just needed to paste the FolderVar inside of the brackets. But i still dont understand why.

The thing is how class-based functions or methods in Lua is declared and called.

When you declare the function:

function Class.foo(self)
     -- code here
end

is the same as

function Class:foo()
    -- code here
end

You can also infer this to calling too. When calling the class-based functions, you need to pass the self parameter manually, class.foo(class) and self refers to the class itself. The said calling method is the same as class:foo(). This does not apply to functions that are not declared inside of a class(or table for that matter) and you shouldn’t add any self to those functions.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.