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

Trouble using Color3 Values?

Asked by 8 years ago

I was making a GUI script for my RPG, which basically makes it so that I have 3 NumberValues: ColourRValue ColourBValue ColourGValue

I wanted to make a script where if you change any of the values, it will change the GUI buttons' BackgroundColor3 value. The problem is, Color3 values don't work up to 255, so I had to divide it by 255. This is where my problem comes in. I divided it but the script doesn't work. Here is my code

local player = script.Parent.Parent.Parent.Parent
local character = player.Character or player.CharacterAdded:wait()
local colourR = script.Parent.ColourRValue --player.leaderstat.ColourR
local colourG = script.Parent.ColourGValue --player.leaderstat.ColourG
local colourB = script.Parent.ColourBValue --player.leaderstat.ColourB
local newColourR = colourR.Value / 255
local newColourG = colourG.Value / 255
local newColourB = colourB.Value / 255

local tut = script.Parent.TutButton
local stats = script.Parent.StatsButton
local skills = script.Parent.SkillsButton
local inv = script.Parent.InvButton
local settings = script.Parent.SettingsButton

colourR.Changed:connect(function()
    local newColourR = colourR.Value / 255
    local newColourG = colourG.Value / 255
    local newColourB = colourB.Value / 255
end)
colourG.Changed:connect(function()
    local newColourR = colourR.Value / 255
    local newColourG = colourG.Value / 255
    local newColourB = colourB.Value / 255
end)
colourB.Changed:connect(function()
    local newColourR = colourR.Value / 255
    local newColourG = colourG.Value / 255
    local newColourB = colourB.Value / 255
end)

newColourR.Changed:connect(function()   

        tut.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        stats.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        skills.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        inv.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        settings.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)

        tut.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        stats.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        skills.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        inv.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        settings.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)

end)

newColourG.Changed:connect(function()   

        tut.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        stats.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        skills.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        inv.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        settings.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)

        tut.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        stats.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        skills.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        inv.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        settings.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)

end)

newColourB.Changed:connect(function()   

        tut.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        stats.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        skills.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        inv.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        settings.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)

        tut.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        stats.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        skills.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        inv.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)
        settings.TextLabel.BackgroundColor3 = Color3.new(newColourR, newColourG, newColourB)

end)

And my error: 11:51:31.696 - Players.Player1.PlayerGui.ScreenGui.Frame.Colour:32: attempt to index local 'newColourR' (a number value)

Also, the hierarchy: https://gyazo.com/f3de31f100dc10720bb25cc008259819

Thanks for your help!

1 answer

Log in to vote
3
Answered by 8 years ago

Consider refactoring your code; there's actually very little reason to be checking so many things that way.

local player = script.Parent.Parent.Parent.Parent
local character = player.Character or player.CharacterAdded:wait()
local colourR = script.Parent.ColourRValue --player.leaderstat.ColourR
local colourG = script.Parent.ColourGValue --player.leaderstat.ColourG
local colourB = script.Parent.ColourBValue --player.leaderstat.ColourB

local tut = script.Parent.TutButton
local stats = script.Parent.StatsButton
local skills = script.Parent.SkillsButton
local inv = script.Parent.InvButton
local settings = script.Parent.SettingsButton

local updateGui = function()   
    local colour = Color3.new(colourR.Value/255,colourG.Value/255,colourB.Value/255);
        tut.BackgroundColor3 = colour;
        stats.BackgroundColor3 = colour;
        skills.BackgroundColor3 = colour;
        inv.BackgroundColor3 = colour;
        settings.BackgroundColor3 = colour;

        tut.TextLabel.BackgroundColor3 = colour;
        stats.TextLabel.BackgroundColor3 = colour;
        skills.TextLabel.BackgroundColor3 = colour;
        inv.TextLabel.BackgroundColor3 = colour;
        settings.TextLabel.BackgroundColor3 = colour;
end;

colourR.Changed:connect(updateGui);
colourG.Changed:connect(updateGui);
colourB.Changed:connect(updateGui);

What changed?
Instead of listening for when the Values change, and then trying to listen for when those values change your new values, this new code simply listens for the value change and updates the Gui in a single function, connected to all of the Changed events.
As a side note, only Instance types have the Changed event.

0
Also, `local player = script.Parent.Parent.Parent.Parent` should be `local player = game.Players.LocalPlayer` instead. XAXA 1569 — 8y
0
Should be, but doesn't have to be; it works just fine as it is (Assuming the hierarchy is as expected) . If the person isn't using FilteringEnabled then they've got the all clear to put Script objects inside of the PlayerGui to be run on the server instead. User#6546 35 — 8y
0
Also, what the Asker was trying to do was check if the variable number has changed rather than the ValueObject. I see you've corrected it in this script, but it would be something to point out to the Asker. M39a9am3R 3210 — 8y
0
"As a side note, only Instance types have the Changed event." User#6546 35 — 8y
View all comments (2 more)
0
Since this is manipulating the player's gui directly, there is no point in moving this to a server script. XAXA 1569 — 8y
0
Not that it matters to me. If it's not broken, it doesn't need fixing. User#6546 35 — 8y
Ad

Answer this question