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(BrickColor.new('Really Red')) -- Really Red

But I want it so that it would output

'BrickColor.new("Really Red")' -- as a string

Now I could just format it simply by doing

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

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 'Vector3.new('..v..')'
	else 
		local to = typeof(v)
		if to == 'Instance' then
			return 'game.'..v:GetFullName()
		elseif to == 'BrickColor' then
			return 'BrickColor.new('..valueise(tostring(v))..')'
		elseif to == 'EnumItem' then
			return tostring(v)
		end
	end
end

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 = BrickColor.new("Really black")
local properties = {["BrickColor"] = 'BrickColor.new("[]")'}
local replaceStringPattern = string.gsub(properties[typeof(yourColor)],"%[]",tostring(yourColor))
print(replaceStringPattern)

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

1 Like