Converting Strings

I was wondering if it was possible to convert strings into another instance; for example, brick colors, objects, etc. If yes, what will I have to do to achieve this?

You can use .new() to convert strings like ”Bright Red“ into BrickColors.

For example:
script.Parent.BrickColor = BrickColor.new(“Bright Red”)

3 Likes

Oh yeah, I figured my problem out: I put plr.TeamColor = color instead of plr.TeamColor = BrickColor.new(color). Also, I think string conversion is not possible actually.

You can convert a string into a BrickColor using the BrickColor constructor if the string is a valid BrickColor (otherwise the BrickColor constructor will return Medium stone grey).

You cannot directly convert a string to a BrickColor just by setting the property equal to the string, you must use the constructor to create a BrickColor.

local BrickColorString = "Black"

Part.BrickColor = BrickColor.new(BrickColorString)

As for objects, you can pass the string into the Instance.new(ClassName) function, where ClassName is a string.

local ClassName = "Part"

local NewPart = Instance.new(ClassName)
NewPart.Parent = workspace

You can see what constructors and functions accept string arguments on the DevHub.

image

5 Likes