Is it possible to change an Instance's property using an string?

Hello! I’m here with another problem for you (Mostly for me as no one replies to my topics :wink:)

So, the title explains everything.

Is it even possible? I tried Instance[Property_Name] = Property but no success as it gives only the child.

Is it even possible without making a long script just with the property names?

Square brackets are just another way of indexing an object using the dot operator.

object["member"] = example
-- is the same as
object.member = example

Therefore, you can indeed index properties by their name but given children are also accessed this way need to make sure Property_Name is a valid property of the instance.

For example:

local part = Instance.new("Part")
part["Size"] = Vector3.new(1, 1, 1)

If you aren’t sure, you can wrap the assignment in a pcall:

local function assign(object, index, value)
  object[index] = value
end

local success = pcall(assign, yourInstance, yourProperty, yourValue)
7 Likes

I forgot that roblox has changed from 2017, sorry :sweat_smile:

1 Like

Another question, I know this probably isn’t possible but can I do the same with functions or can I even do something like this with functions?

Example: workspace.Baseplate"Destroy" (I put that [] but the forum deleted it)

Yes. Like mentioned, a.b is sugar for a["b"]. So that means a:b() is sugar for a.b(a) which itself is sugar for a["b"](a)

1 Like

I will try it, thanks :grin:

I will edit this if it isn’t working.

It isn’t really working.

I tried this:

		local InstanceGot = Args[1]
		local Function = Args[2]
		local FunctionGot = InstanceGot[Function]
		table.remove(Args,1)
		table.remove(Args,2)
		local success,err = pcall(FunctionGot(InstanceGot,unpack(Args)))
		if not success then
			warn(err)
		end
		return success and err or nil

And it just errors

Destroy
is not a valid member of Part “Workspace.Basepart”

InstanceGot is an Instance and Function is an string.

There is no method in the entire Roblox API that returns a function, what you want is pcall(FunctionGot, InstanceGot, unpack(Args))

1 Like

Thanks! That was my last question, I promise :wink:

1 Like