How would I determine if a value is a BrickColor or a Color3Value?


Achieve

I’m attempting to make a light command that can do BrickColor and Color3 Values.

Issue

Just so this is clear, The function works; the issue is that if I have the Color3.fromRGB before the BrickColor.new value in the script… it goes straight to black… it does the same thing if I reverse the code.

Solution

I have no solutions to this issue, my idea was figuring out a way to detect if it’s a number or a letter but that causes issues as well.


Example Script

Light = {Function = function(plr,msg)
			if not game.Workspace[plr.Name].Torso:FindFirstChild("PointLight") then
				local Light = Instance.new("PointLight",game.Workspace[plr.Name].Torso)
				Light.Brightness = 2
				if msg ~= nil then
					Light.Color = Color3.fromRGB(unpack(string.split(msg, ","))) or BrickColor.new(msg).Color
				else
					Light.Color = Color3.fromRGB(200,200,200)
				end
			else
				local Light = game.Workspace[plr.Name].Torso["PointLight"]
				if msg ~= nil then
					Light.Color = Color3.fromRGB(unpack(string.split(msg, ","))) or BrickColor.new(msg).Color
				else
					Light.Color = Color3.fromRGB(200,200,200)
				end
			end
		end}

Thank you for any help you can give me.

Use the Roblox function typeof

print(typeof(BrickColor.new()))
3 Likes

Instead of adding a direct overload for the parameter, how about make a different parameter that specifies the type of input the player is about to use.

:Light Color3 122, 233, 451

Alternatively, the lazy way is to just use pcall and see if you’re allowed to construct a brick color / color3 with the parameters they passed. Or just try counting the amount of parameters

local R, G, B = unpack(msg:split(",")) --// Under the assumption msg will be something like "255,122,192"

R = tonumber(R)
G = tonumber(G)
B = tonumber(B)

if (R and G and B) then
    local Color3 = Color3.new(R, G, B)
else
    --// this might be tricky because it can be a 'brick color code' or a 'brick color name'. I'm about to leave for school so hopefully you can figure this out on your own.
end
1 Like

Is this what you meant by using the typeof function?

if typeof(BrickColor.new(msg).Color) then
						Light.Color = BrickColor.new(msg).Color
					else
						Light.Color = Color3.fromRGB(unpack(string.split(msg, ",")))
					end
				else
					Light.Color = Color3.fromRGB(200,200,200)
				end

If you want to change a part’s color but dont know which datatype it is, do it like this:

part[typeof(colorVal) == "BrickColor" and "BrickColor" or typeof(colorVal) == "Color3" and "Color"] = colorVal
1 Like

I sadly did not want to grab/change a parts color type, i’m setting a PointLights color through a string value that would either be a Color3 Value or a BrickColor Value. Thank you either way.