Problem while using tonumber() inside Color3.fromRGB

Hello!

Some time ago I made a Rock Placer plugin, but yesterday I decided to rewrite it, redo the UI, and add new functions like choosing the color of the stone. The idea is to have three TextBox and each one represents an RGB value, one is R, one is G and so on.
image

Solution while using RGB in BrickColor

Just making it clear that I solved it, the important thing is problem 2.

I was trying to use RGB in BrickColor:

Rock.BrickColor = BrickColor.new (255, 255, 255)

It didn’t generate the desired color, so I went here to search DevForum and found something about BrickColor.new(Color3). So I tried:

Rock.BrickColor = BrickColor.new (Color3.fromRGB(255, 255, 255))

And it didn’t work either. But I tried another solution, looking at the properties of BaseParts, I realized that besides BrickColor they have Color:

Rock.Color = Color3.fromRGB(255, 255, 255)

It worked. I just don’t know if it’s good practice to use it, so I ask for help here.


Using the first solution I made half of the script work, but I couldn’t make it work with the tonumber() function inside a Color3:

local R = ColorR.TextBox.Text
local G = ColorG.TextBox.Text
local B = ColorB.TextBox.Text
	
if not R == "" or not G == "" or not B == "" then --> Verify if the TextBox text isn't nil
	Object.Color = Color3.fromRGB(tonumber(R), tonumber(G), tonumber(B)) --> Where the problem goes!
else
	Object.Color = Color3.fromRGB(99, 95, 98) --> It works
end

This does not give me any errors, but also does not work, I do not know if I misused the tonumber() or something, I have nothing to talk about. But when I write some number in the TextBox it gives me a black color. But the else of the script works and generates the desired color.


Also, do you know any method of preventing people from writing letters in a TextBox? and allow only Numbers?

Thank you for reading this!

This is my first topic in this category, I’m not a dedicated programmer either.

You can use GetPropertyChangedSignal("Text") to get the event for whenever the text changes, and edit the text property to delete anything that is not a number whenever this signal fires:

local textBox = -- your text box

local conn = textBox:GetPropertyChangedSignal("Text"):Connect(function()

    -- looks for anything that is NOT a number
    -- and replaces it with an empty string
    textBox.Text = textBox.Text:gsub("%D+", "")
end)

You could simply add three of these for each of your text boxes (or use a for loop), and you would also need to disconnect the connection if you plan on not destroying those text boxes.

2 Likes

This is easier than you think! We just need to remove non-numeric characters whenever the text is changed!

-- Typed on mobile :(

ColorG.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
  ColorG.TextBox.Text = string.gsub(ColorG.TextBox.Text, "%D+", "")
end)

Edit: Reply above beat me to this by a second. GG bro


In regards to your main problem, your conditional logic seems flawed. You’re using or instead of and. Wouldn’t you want to only create a Color3 if they’ve provided all three inputs, not just one? With your current version, they could put a number for G but leave R and B blank and your code would do Color.fromRGB(nil, G_Number_Value, nil) and that would cause this unintended behavior.

1 Like

@goldenstein64 @boatbomber Thank you both for answering me here, I will try to apply these solutions to my code. Also I would like to know if you know any solution to detect if a number is not nil, because when using tonumber ("") we get a nil value instead of 0, and this generated some previous problems.

local Text = TextBox.Text

if tonumber(Text) = nil then
--> code
end

It doesn’t work for me, does you know the solution?

Use an or to default to 0 when nil, and remember to clamp between 0 and 255 so players don’t input wacky values!

local Num = math.clamp(tonumber("") or 0, 0,255)
1 Like

Combining everything we’ve discussed, we end up with this:

-- Still on mobile, you'll have to clean this up

local function ColorFromInputs()
 local R = ColorR.TextBox.Text
 local G = ColorG.TextBox.Text
 local B = ColorB.TextBox.Text

 return Color3.fromRGB(
  math.clamp(tonumber(R) or 0, 0,255),
  math.clamp(tonumber(G) or 0, 0,255),
  math.clamp(tonumber(B) or 0, 0,255)
 )

end


ColorR.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
  ColorR.TextBox.Text = string.gsub(ColorR.TextBox.Text, "%D+", "")
  Object.Color = ColorFromInputs() or Color3.new()
end)

ColorG.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
  ColorG.TextBox.Text = string.gsub(ColorG.TextBox.Text, "%D+", "")
  Object.Color = ColorFromInputs() or Color3.new()
end)

ColorB.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
  ColorB.TextBox.Text = string.gsub(ColorB.TextBox.Text, "%D+", "")
  Object.Color = ColorFromInputs() or Color3.new()
end)
	

1 Like