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

Transferring color to brick with click of a button?

Asked by 5 years ago

On Roblox, there's a color palette that you can drag around to find the right color. The script below is a replication of that, for in-game purposes. When someone is dragging around their mouse as they choose the color they'd like, an output text shows. For example, "Medium Stone Grey" will show an output text of (163, 162, 165). I added an "ok" button to this GUI attempting to make it so that the color picked transfers over to the brick -(script below the first) when this button is clicked. BUT, I'm having trouble doing this. Please please please please tell me what I'm doing wrong with the "ok" button part of the script; I have no idea how to piece it together.

----nothing is wrong with this script, this is just to show the functions of the script inside the color GUI
function waitForChild(instance, name) if instance:FindFirstChild(name) then return instance:FindFirstChild(name) end
repeat wait() until instance:FindFirstChild(name) return instance:FindFirstChild(name) end
function waitForProperty(instance, name) if instance[name] then return instance[name] end repeat wait() until instance[name] return instance[name] end

local mouse = waitForProperty(game:service("Players"), "LocalPlayer"):GetMouse()
local index = waitForProperty(script, "Parent")
local spectre = waitForChild(index, "Spectre")
local cursor = waitForChild(spectre, "Cursor")
local preview = waitForChild(index, "Preview")
waitForChild(preview, "Col1") waitForChild(preview, "Col2")
local sat = waitForChild(index, "Satuation")
local cursor2 = waitForChild(sat, "Cursor")
local isDown = {} -- For dragging purpose.
local data = {{0, 1, 0}, {0, 1, 1}} -- Storing colors.

game:service("ContentProvider"):Preload(cursor.Image)

local output = waitForChild(index, "Output")

function HSVtoRGB(rawh, rawm, rawv, setColor)

local h = ((rawh - spectre.AbsolutePosition.X) / spectre.AbsoluteSize.X)
local v = ((rawv - spectre.AbsolutePosition.Y) / spectre.AbsoluteSize.Y)
local s = 1
local m = (rawm and 1 - ((rawm - sat.AbsolutePosition.Y) / sat.AbsoluteSize.Y)) or 1

v = (1 - v) * 2

if h > 1 then h = 1 elseif h < 0 then h = 0 end if v > 2 then v = 2 elseif v < 0 then v = 0 end if m > 1 then m = 1 elseif m < 0 then m = 0 end

  local r, g, b

  local i = math.floor(h * 6)
  local f = h * 6 - i
  local p = v * (1 - s)
  local q = v * (1 - f * s)
  local t = v * (1 - (1 - f) * s)

  local switch = i % 6
  if switch == 0 then
    r = v g = t b = p
  elseif switch == 1 then
    r = q g = v b = p
  elseif switch == 2 then
    r = p g = v b = t
  elseif switch == 3 then
    r = p g = q b = v
  elseif switch == 4 then
    r = t g = p b = v
  elseif switch == 5 then
    r = v g = p b = q
  end

  r, g, b = math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)

  if v > 1 then
    r = r + (255 - r) * (v - 1)
    g = g + (255 - g) * (v - 1)
    b = b + (255 - b) * (v - 1)
  end

  if r > 255 then r = 255 end if g > 255 then g = 255 end if b > 255 then b = 255 end

  sat.BackgroundColor3 = Color3.new(r / 255, g / 255, b / 255)

  r, g, b = math.floor(128 - (128 - r) * m), math.floor(128 - (128 - g) * m), math.floor(128 - (128 - b) * m)

  if r > 255 then r = 255 end if g > 255 then g = 255 end if b > 255 then b = 255 end

  cursor.Position = UDim2.new(h, -5, -(v / 2) + 1, -5)
  cursor2.Position = UDim2.new(0, 0, 1 - m, 0)

output.Text = tostring(table.concat({r, g, b}, ", "))
if setColor then

preview["Col" .. setColor].BackgroundColor3 = Color3.new(r / 255, g / 255, b / 255)
data[tonumber(setColor)][1] = rawh
data[tonumber(setColor)][2] = m
data[tonumber(setColor)][3] = rawv

end

  return Color3.new(r / 255, g / 255, b / 255)

end

spectre.MouseButton1Down:connect(function(x, y) isDown["h1"] = true HSVtoRGB(x, nil, y, "1") end)
spectre.MouseButton2Down:connect(function(x, y) isDown["h2"] = true HSVtoRGB(x, nil, y, "2") end)
mouse.Move:connect(function() if isDown["h1"] then HSVtoRGB(mouse.X,nil,mouse.Y,"1") elseif isDown["h2"] then HSVtoRGB(mouse.X,nil,mouse.Y,"2")
elseif isDown["s1"] then HSVtoRGB(data[1][1], mouse.Y, data[1][3], "1") elseif isDown["s2"] then HSVtoRGB(data[2][1], mouse.Y, data[2][3], "2") end end)
spectre.MouseButton1Up:connect(function() isDown["h1"] = nil end) mouse.Button1Up:connect(function() isDown["h1"], isDown["s1"] = nil, nil end)
spectre.MouseButton2Up:connect(function() isDown["h2"] = nil end) mouse.Button2Up:connect(function() isDown["h2"], isDown["s2"] = nil, nil end)
sat.MouseButton1Up:connect(function() isDown["s1"] = nil end) sat.MouseButton1Down:connect(function(x, y) isDown["s1"] = true
HSVtoRGB(data[1][1], y, data[1][3], "1") end)
sat.MouseButton2Up:connect(function() isDown["s2"] = nil end) sat.MouseButton2Down:connect(function(x, y) isDown["s2"] = true
HSVtoRGB(data[2][1], y, data[2][3], "2") end)
--script inside "ok" button
local ok = script.Parent
local brick = game.Workspace.Brick
local output = script.Parent.Parent.Output

ok.MouseButton1Click:connect(function()
    output.text = brick.Color
            wait()

        end

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
5 years ago
Edited 5 years ago

To communicate between local scripts you should use bindable events, functions or at the very least local global variables. Attempting to convert text back to color vector would give me a huge headache. Global variable would be easier, but they tend to get harder to manage once your game grows in size. Bindable event solution here, insert this code at the beginning or your first script:

local ChangeColor = Instance.new("BindableEvent")
ChangeColor.Name = "ChangeColor"
ChangeColor.Parent = game.Players.LocalPlayer

Then add this in line number 83 (before return)

ChangeColor:Fire(Color3.new(r / 255, g / 255, b / 255))

Here is your rewritten OK button script:

--script inside "ok" button
local ok = script.Parent
local brick = game.Workspace.Brick
local output = script.Parent.Parent.Output
local ChangeColor = game.Players.LocalPlayer:WaitForChild("ChangeColor")
local selectedColor = Color3.new(0,0,0) --always try to avoid null cases

ok.MouseButton1Click:Connect(function()
    output.text = selectedColor.r .. selectedColor.g .. selectedColor.b
    brick.Color = selectedColor
            wait()

        end)

ChangeColor.Event:Connect(function(NewColor)
    selectedColor = NewColor
end)

Unfortunately this would probably not work as intended, if the Filtering Enabled is on. The brick would change color only for local player, on server and other clients it will remain the same. Let me know if you need to adjust the script to replicate change in the shared workspace.

0
Your guidance made a lot of sense! Thanks! Unfortunately, the script does not work :( Thanks anyway! LiLFriks 39 — 5y
0
BTW, I know you will probably die inside, but you instead of writing your own, you can use Color3.fromHSV ... sleazel 1287 — 5y
Ad

Answer this question