Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to change a property of an instance using a string as the name?

I've looked around and couldn't find anything, but I basically mean if there's a way to do something like

Part:Change('Transparency', 0.75)

If this doesn't make sense I'm sorry, I'll try to answer any questions.

2 comments

0

Why exactly you need this?

0

I'm trying to make it where in a game you can put a number or bool value inside a part and it sets the parts value (Name of the part) to the value of the variable, eg. a number value called Transparency with a value of 0.75

1 answer

✓ Accepted answer

Properties of a userdata value are handled like indices. In other words, while this works...

part.Transparency = 1

...this also works:

part["Transparency"] = 1

In fact, because userdata properties are handled as if the userdata was a table (which they actually are), you can do this with quite literally any property. Unfortunately you can't tell the compiler to explicitly treat a userdata like a table.

However, since userdata values aren't explicit tables, something like this doesn't work:

print(#workspace.Baseplate)

But this does:

print(workspace.Baseplate["BrickColor"])

With that being said, you can make a table of strings for each specific Instance and check to see if that exists in the userdata:

local partProperties = {"Size", "BrickColor", "Transparency", "Parent"}
local rand = partProperties[math.random(1, #partProperties)]
if workspace.Baseplate[rand] then -- This actually works
	print(workspace.Baseplate[rand])
end

That is how you would assess it.

This discussion is preserved as it was. New answers, comments, and votes are closed.