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

[SOLVED] Converting strings into Color3?

Hi everybody,

I'm making a system where you enter in a Color3 value (one in each box) and all the lights around you tween to the colour that was put in the textboxes. The obvious issue is each value is a string and not a number which therefore can't be used in a Color3Value. What would be the best way to convert a string into a Color3 that can be used later, I have a bit of code so you can see what I'm getting at (I'm probably going in the wrong direction

local AdvancedMode = script.Parent
local BasicMode = AdvancedMode.Parent.BasicMode
local modetoggle = AdvancedMode.Parent.modetoggle
local confirmbtn = AdvancedMode.confirmbutton
local inputR = AdvancedMode.inputR
local inputG = AdvancedMode.inputG
local inputB = AdvancedMode.inputB
local CurrentColour = AdvancedMode.CurrentColour

confirmbtn.MouseButton1Click:Connect(function()
	print('test')
	local inputR_numb = inputR.Text
	local inputG_numb = inputG.Text
	local inputB_numb = inputB.Text
	print(inputR_numb,inputG_numb,inputB_numb)
	local combo = inputR_numb..','..inputG_numb..','..inputG_numb
	--combo = tonumber(combo) tried, doesn't work.
	CurrentColour.Value = combo
end)

The error I get when I run this (with tonumber bit) invalid argument #3 (Color3 expected, got nil) The error I get without the tonumber bit invalid argument #3 (Color3 expected, got string) Same errors but at least I know I'm probably on the right track with tonumber. That's pretty much everything

3 comments

0

Have you tried Color3.new(inputR.Text,inputG.Text,inputB.Text)? (and with tonumber aswell)

0

I would also check to see if the input is a number with the inbuilt function tonumber() this function returns nill if you input letters. It should also work with float values too!.... EG print(tonumber("0123")) -- 123 Print(tonumber("0.123")) -- 0.123 print(tonumber("hello") -- nil

0

Problem solved, pretty sure I created a new color3 and ran math.ceil on each value

1 answer

you can use tonumber()

but there is some problems if you only use tonumber()

  1. if the player use any invalid character, there is gonna be an error
  2. if the player use a comma instead of a dot, there is gonna be an error and the player will be confused
  3. if the number is higher than 255 or lower than 0, it will error

so you can use this function to convert 3 strings in a color3 value

local function toColor3(r,g,b) -- takes 3 strings for args
	r = r:gsub(",","%.",1)
	g = g:gsub(",","%.",1)
	b = b:gsub(",","%.",1)
	
	r = tonumber(r); g = tonumber(g); b = tonumber(b)
	
	if r == nil or g == nil or b == nil then return false end
	
	return Color3.fromRGB(
		math.clamp(r,0,255),
		math.clamp(g,0,255),
		math.clamp(b,0,255)
	)
end

-- the function return the Color3 value or false if the strings are not valid

(btw i would use values from 0 to 255 instead of 0 to 1)

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