Changing UI Color won't work?

I’m trying to make a dark mode for people who don’t want to have the light mode, but the script has an error and I’m a beginner for scripting, it’s a normal script.
Edit: Another problem from changing it back to light mode is saying “If” is wrong.
Image:


Please tell me if anything is wrong.

change your local player variable from local.player to game.Players.LocalPlayer

and use Color3.new instead of just saying the color value

You need to change

local player = local.player

to

local player = game.Players.LocalPlayer

and change

Create.BackgroundColor3 = (30, 24, 150)

to

Create.BackgroundColor3 = Color3.fromRGB(30, 24, 150)

Explanation for the things above:

  1. Game.Players.LocalPlayer is the proper way to get the local player as long as you are using a local script.

  2. Color3.fromRGB() is used to create new color values which can they be applied to UIs

1 Like

You are using a script. Use a Localscript, try then.

Script is a server-script, doesn’t work on clients, LocalScript works on clients.

Even after switching to a local script, I still have the same error, I tried solving it, but now the last end is the problem.
Picture:

you’re missing two end’s in your code. One for the first if statement and one for the second if statement.

Where, can you show me please?

Copy paste your code here

local Website = script.Parent.Parent.Parent
local player = game.Players.LocalPlayer
local Create = script.Parent.Parent.Parent.Create
local Friends = script.Parent.Parent.Parent.Friends
local Games = script.Parent.Parent.Parent.Games
local Home = script.Parent.Parent.Parent.Home
local Settings = script.Parent.Parent.Parent.Settings
if script.Parent.Parent.Parent.DarkMode.Value == false then
	script.parent.MouseButton1Click:Connect(function()
		wait (0.1)
		Create.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
		Friends.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
		Games.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
		Home.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
		Settings.BackgroundColor3 = Color3.fromRGB(6, 5, 34)


end)
if script.Parent.Parent.Parent.DarkMode.Value == true then
script.Parent.Text = "Light Mode"
script.Parent.MouseButton1Click:Connect(function()
			wait (0.1)
	Create.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Friends.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Games.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Home.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Settings.BackgroundColor3 = Color3.fromRGB(255, 255, 255)


end)

local Website = script.Parent.Parent.Parent
local player = game.Players.LocalPlayer
local Create = script.Parent.Parent.Parent.Create
local Friends = script.Parent.Parent.Parent.Friends
local Games = script.Parent.Parent.Parent.Games
local Home = script.Parent.Parent.Parent.Home
local Settings = script.Parent.Parent.Parent.Settings

script.parent.MouseButton1Click:Connect(function()

  if script.Parent.Parent.Parent.DarkMode.Value == false then
    wait (0.1)
    Create.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
    Friends.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
    Games.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
    Home.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
    Settings.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
    script.Parent.Parent.Parent.DarkMode.Value = true
  else
    script.Parent.Text = "Light Mode"
    wait (0.1)
    Create.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Friends.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Games.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Home.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Settings.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    script.Parent.Parent.Parent.DarkMode.Value = false
  end
end)

Made it simpler too. Your if statements are supposed to be inside the event so that they fire every time the UI is clicked. Then you can do an if-else instead of two if statements

1 Like

if script.Parent.Parent.Parent.DarkMode.Value == false then

You can change this part to:
if not script.Parent.Parent.Parent.DarkMode.Value then

(Just a little cleaner).

Two by-the-by tips:

  1. You can drop the double equals true when evaluating truthiness; if <value> is equivalent to if <value> == true, and you can similarly use if not <value> to evaluate if something is false.
  2. I noticed you’re running the same operation on multiple instances that share the same parent; instead of repeating the same code, you can use :GetChildren() and loop through the results like so:
for _, element in pairs(script.Parent.Parent.Parent:GetChildren()) do
    element.BackgroundColor3 = Color3.fromRGB(6, 5, 34)
end

Hope to see you around more, and good luck!

Edit: You’ll want to move your DarkMode value elsewhere if you use the above loop, as value objects don’t have a BackgroundColor3 property and it will cause an error.