Button to turn computer screen on and off

This script should simply just turn a screen black and if its already black turn the screen white, basically making the impression of a screen turning on and off. Once i press the button though, it becomes “248, 248, 248” instead of the correct color, this way it doesnt do anything after pressing it once.

local screen = workspace.Screen.Monitor
local button = workspace.Screen.Part.ClickDetector

local currentColor = Color3.new(157, 157, 157)
local defaultColor = Color3.new(0, 0, 0)

local function changeColor()
    if screen.BrickColor.Color == currentColor then
        screen.BrickColor = BrickColor.new(defaultColor)
    else
        screen.BrickColor = BrickColor.new(currentColor)
    end
end

button.MouseClick:Connect(changeColor)
3 Likes

Maybe try instead of setting Color3.new do:

screen.BrickColor = BrickColor.new() --your value of color
2 Likes

wouldn’t screen.Color3 = [put Color3.new here] fix it?

2 Likes

Its not working because your doing screen.BrickColor = BrickColor.new but the variable currentColor and defaultColor are color3 variable types. You either need to do
local currentColor = “Black”
local defaultColor = “White”

Or if u want to use color3 then u need to change
screen.BrickColor to screen.Color = Color3.new(0, 0, 0)
Something like that. I didnt do this in studio so not sure if all my syntax is correct

1 Like

Your problem with BrickColor is it’s returning a BrickColor, in this case: Institutional white. You could change the BrickColor or the Color3. In this case you’re looking to use (157, 157, 157) and that is being used as RGB format. This is a simple flag toggle switch using Color3.fromRGB() …

local screen = workspace.Screen.Monitor
local button = workspace.Screen.Part.ClickDetector
local switch = false

local function changeColor()
	if switch == false then switch = true
		screen.Color = Color3.fromRGB(157, 157, 157)
	else screen.Color = Color3.fromRGB(0, 0, 0)
		switch = false
	end
end

button.MouseClick:Connect(changeColor)
2 Likes

The Color3 values you are using for currentColor and defaultColor are specified with RGB values in the range of 0 to 255, but the BrickColor constructor expects values in the range of 0 to 1.

To resolve the issue, you need to divide the RGB values of currentColor and defaultColor by 255. By dividing the RGB values by 255, you ensure that the colors are in the correct range. Now the script should toggle the screen color between black and white when the button is clicked. Here’s the corrected code:

local button = workspace.Screen.Part.ClickDetector

local currentColor = Color3.new(157/255, 157/255, 157/255)
local defaultColor = Color3.new(0, 0, 0)

local function changeColor()
    if screen.BrickColor.Color == currentColor then
        screen.BrickColor = BrickColor.new(defaultColor)
    else
        screen.BrickColor = BrickColor.new(currentColor)
    end
end

button.MouseClick:Connect(changeColor)
1 Like

Many call this a debounce It is really called a flag. You can use a flag as a “debounce”, but flags have many uses.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.