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

How do I make a textlabel change color when clicked?

Asked by 1 year ago

The title isnt exactly what I cant do but its close. I want the text to progressively change from red to green over the RGB spectrum. I have tried but at the end it starts glitching and changes colors to light green and the green that I want. My current code is this

local parts = script.Parent.Parent:WaitForChild("Collectables"):GetChildren()
local red = 255
local green = 0
local blue = 0

for i, v in pairs(parts) do
    if v:IsA("Part") then
        print(v.Name.." IS REGISTERED")
        if v:WaitForChild("ClickDetector") then
            v.ClickDetector.MouseClick:Connect(function()
                print("clicked "..v.Name)
                if v:FindFirstChild("SurfaceGui") then
                    v.SurfaceGui.TextLabel.Text = v.SurfaceGui.TextLabel.Text + 1
                    if blue < 8 then
                        if green < 250 then
                            green += 50
                        elseif green == 250 then
                            green += 5
                        elseif green == 255 then
                            red -= 50
                        elseif red == 0 then
                            blue += 1
                        end
                    end
                    local color = Color3.fromRGB(red,green,blue)
                    v.SurfaceGui.TextLabel.TextColor3 = color
                end
            end)
        end
    end
end

Answer this question