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
you can use tonumber()
but there is some problems if you only use tonumber()
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)