Having a little bit of trouble with ClickDetectors

Hey there, I am having trouble with a ClickDetector System to turn a laptop model on and then back off. I am a beginner scripter, here on ROBLOX. The idea of the script is for when I click the screen of the laptop, it will change color and material, and when I click it again, it will change back.

I am getting no errors in the output.

Here is the code:
ClickDetectorscriptingsupport

Here is my explorer setup as well:
https://gyazo.com/e8cd6ca674f307e2449643cb51c1f115

Can you post your script instead of an image, please?

Sure thing!

local CD = script.Parent.ClickDetector
local Screen = game.Workspace.Props.Laptop.ScreenPart

local ScreenColor = Screen.BrickColor

local Off = Screen.BrickColor == BrickColor.new(27, 42, 53)
local On = Screen.BrickColor == BrickColor.new(205, 205, 205)

if Off == true then
	CD.MouseClick:Connect(function()
	Screen.BrickColor = BrickColor.new(205, 205, 205)
	Screen.Material = Enum.Material.Neon
end)
end

if On == true then
	CD.MouseClick:Connect(function()
	Screen.BrickColor = BrickColor.new(27, 42, 53)
	Screen.Material = Enum.Material.SmoothPlastic
end)
end

I also changed lines 10 and 17, as I wasn’t using my “CD” Variable.

Hm, instead of checking if it’s On/Off outside of the connections, why not check it inside of just 1 connection?

local CD = script.Parent.ClickDetector
local Screen = game.Workspace.Props.Laptop.ScreenPart

CD.MouseClick:Connect(function()
	if Screen.BrickColor == BrickColor.new(27, 42, 53) then -- Off
		Screen.BrickColor = BrickColor.new(205, 205, 205)
		Screen.Material = Enum.Material.Neon
	elseif Screen.BrickColor == BrickColor.new(205, 205, 205) then -- On
		Screen.BrickColor = BrickColor.new(27, 42, 53)
		Screen.Material = Enum.Material.SmoothPlastic
	end
end)

I tried rewriting your script and I made this, works fine on my behalf

local on = false

local CD = script.Parent.ClickDetector

local onColor = Color3.new(205/255, 205/255, 205/255)

local offColor = Color3.new(27/255, 42/255, 53/255)

local Screen = workspace.Props.Laptop.ScreenPart

CD.MouseClick:connect(function(click)

on = not on

if on == false then

Screen.Color = offColor

Screen.Material = Enum.Material.SmoothPlastic

else

Screen.Color = onColor

Screen.Material = Enum.Material.Neon

end

end)
2 Likes

Nice work. Can you explain how the “else” works?

Edit: And does it matter if you use Color3 vs BrickColor?

In the if function? Since we’re looking for if the “on” variable is false, else basically executes when the variable isn’t what the if function is looking for.

In this case the else is being used to fire when the “on” variable is anything but false. If you want it to specifically fire when the variable is true, you can use “elseif on == true then” etc.

You can find more in this page here.

BrickColor is also used for BrickColor values (eg: part colour names like “Really red”, “Black”), Color3 is defining a specific RGB colour code for the part, you can use either.

1 Like