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!
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.