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 9 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

01local player = script.Parent.Parent.Parent.Parent
02local character = player.Character or player.CharacterAdded:wait()
03local colourR = script.Parent.ColourRValue --player.leaderstat.ColourR
04local colourG = script.Parent.ColourGValue --player.leaderstat.ColourG
05local colourB = script.Parent.ColourBValue --player.leaderstat.ColourB
06local newColourR = colourR.Value / 255
07local newColourG = colourG.Value / 255
08local newColourB = colourB.Value / 255
09 
10local tut = script.Parent.TutButton
11local stats = script.Parent.StatsButton
12local skills = script.Parent.SkillsButton
13local inv = script.Parent.InvButton
14local settings = script.Parent.SettingsButton
15 
View all 78 lines...

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 9 years ago

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

01local player = script.Parent.Parent.Parent.Parent
02local character = player.Character or player.CharacterAdded:wait()
03local colourR = script.Parent.ColourRValue --player.leaderstat.ColourR
04local colourG = script.Parent.ColourGValue --player.leaderstat.ColourG
05local colourB = script.Parent.ColourBValue --player.leaderstat.ColourB
06 
07local tut = script.Parent.TutButton
08local stats = script.Parent.StatsButton
09local skills = script.Parent.SkillsButton
10local inv = script.Parent.InvButton
11local settings = script.Parent.SettingsButton
12 
13local updateGui = function()  
14    local colour = Color3.new(colourR.Value/255,colourG.Value/255,colourB.Value/255);
15        tut.BackgroundColor3 = colour;
View all 30 lines...

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 — 9y
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 — 9y
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 — 9y
0
"As a side note, only Instance types have the Changed event." User#6546 35 — 9y
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 — 9y
0
Not that it matters to me. If it's not broken, it doesn't need fixing. User#6546 35 — 9y
Ad

Answer this question