Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to work this IntConstrainedValue into a function?

Asked by 8 years ago
-- In a LocalScript in StarterGui:

-- Create ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent

-- Insert RGBlist number count on this line

-- Create ColorBox
local colorBox = Instance.new("Frame")
colorBox.Name = "Color"
colorBox.Parent = screenGui
colorBox.Position = UDim2.new(0, 0, 0.5, -75)
colorBox.Size = UDim2.new(0, 150, 0, 150)
colorBox.BackgroundColor3 = Color3.fromRGB(redValue,greenValue,blueValue)

local RGBlist = Instance.new("IntConstrainedValue",game.StarterGui)
RGBlist.Name = "RGB Possible Value List"
RGBlist.MaxValue = 255
RGBlist.MinValue = 0
RGBlist.Value = 0

-- Create RedValue
local redValue = 0

-- Create RedInput
local redInput = Instance.new("TextBox")
redInput.Name = "Red"
redInput.Parent = screenGui
redInput.Position = UDim2.new(0.5,-100,0.5,-75)
redInput.Size = UDim2.new(0,200,0,50)
redInput.BackgroundColor3 = Color3.new(255,0,0)

function redText()
    if redInput.Text <=0 or redInput.Text == ("-" .. math.random()) or redInput.Text == math.random() then 
        redValue = 0
    elseif redInput.Text == RGBlist.Value then
        redValue = RGBlist.Value
    end
end

redText()

I wanted to make sure that if someone typed a value between 0 and 255 into a text box, it would default to that number for a certain int value in the script. But, the way I typed it wouldn't work, and I have no idea of how to fix it.

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

You can use the FocusLost method of TextBox to get the string that the user inputted. We can then use the function tonumber to turn that string into a number (if possible, else returns nil). Then we simply set the BackgroundColor3 value of the GUI object you wish to change to the new redValue and the old green and blue values.

Example:

textBox.FocusLost:connect(function(enterPressed)
    if enterPressed then
        local value = tonumber(textBox.Text)
        if value then
            local oldColor= gui.BackgroundColor3
            gui.BackgroundColor3 = Color3.fromRGB(value, oldColor.g, oldColor.b)
        end
    end
end)

Assumes that textBox is the Textbox in which you want to pull the value from and gui is the GUI object you wish the change the color of.

Ad

Answer this question