Any way to convert properties to strings for source automatically

The titles a bit messy but I’ll explain

If I want the value of a parts BrickColor I just do Part.BrickColor, but when I use print or tostring it returns just the string of the BrickColor.

print('Really Red')) -- Really Red

But I want it so that it would output

'"Really Red")' -- as a string

Now I could just format it simply by doing

if typeof(v) == 'BrickColor' then
  return ''..tostring(v)..')'

But that would take a lot of effort so I wondered if there was an automated way to do this or even a built in way.

I’ll probably have to resort to my previous method for many different types but I’m sorta lazy and want a nice code instead of a bunch of elseif s

My simple solution right now;

local function valueise(v)
	local t = type(v)

	if t == 'string' then
		return tostring('"'..v..'"')
	elseif t == 'number' or t == 'boolean' then
		return tostring(v)
	elseif t == 'vector3' then
		return ''..v..')'
		local to = typeof(v)
		if to == 'Instance' then
			return 'game.'..v:GetFullName()
		elseif to == 'BrickColor' then
			return ''..valueise(tostring(v))..')'
		elseif to == 'EnumItem' then
			return tostring(v)

You could try by putting each property in a table and assigning a string to the property, then use gsub to replace a string pattern inside the brackets. For example

local yourColor ="Really black")
local properties = {["BrickColor"] = '"[]")'}
local replaceStringPattern = string.gsub(properties[typeof(yourColor)],"%[]",tostring(yourColor))

This will print"Really black") and hopefully should save a lot of time and replace the need for lots of elseifs.

1 Like