Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this Color3 code not functioning properly? [3 lines]

I'm making a plugin basically using this functionality but it's over 300 lines so I'm not going to post that code here, so I'll just make a quick test, so in my plugin I can insert three numbers as so "255, 255, 255" and then when they press enter (focus lost) it will set a parts color to the text of the text box, but it turns it into 0, 0, 0 for some reason.

Here is what I'm trying to do:

script.Parent.FocusLost:Connect(function()
	workspace.Baseplate.Color = Color3.fromRGB(script.Parent.Text)
end)

It turns the baseplate's Color to 0, 0, 0 instead of what I inputted which was "255, 0, 255" into the text box, what is the issue here?

1 comment

0

fromRGB takes 3 integers in the range of 0~255

2 answers

Well, you're trying to construct a Colour3 value from a string - you can't do that.

Use this function to help:

function getNumbers(str, start)
       local place
       local tbl = {}
       for i = start, #str do
              if str:sub(i,i) == ',' and not place then
                     place = i
              end
       end
       return tonumber(str:sub(start, place-1)), place
end

function getColour3(str)
       local r, g, b
       local start
       r, start = getNumbers(str, start and start or 1)
       wait()
       g, start = getNumbers(str, start+1)
       wait()
       b, start = getNumbers(str, start+1)
       return Color3.new(r/255, g/255, b/255)
end)

script.Parent.FocusLost:Connect(function()
        workspace.Baseplate.Color = getColour3(script.Parent.Text)
end)

Hasn't been tested, but I'm 45% sure that this will work

I think that you have a small mistake (I have done the same thing), where you are assigning it to an object that doesn't have color, so it assumes it is (0,0,0). Instead I think you should assign it to the Text Color. Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.