Value not changing by ClickDetector

I’m making a light switch that operates through value. I want the game to know to turn the lights on when they’re off and vice versa, but I click the switch and nothing happens. Am I doing something wrong with my script or is it set up poorly?

local switch = script.Parent
local off = game.Workspace.Bedroom.Light.Light.Off
local on = game.Workspace.Bedroom.Light.Light.On
local value = game.Workspace.Values.LightBD.Value

script.Parent.Parent.ClickDetector.MouseClick:Connect(function(player)
	if value == 0 then
		on:Play()
		value += 1
	end
	if value == 1 then
		off:Play()
		value -= 1
	end
end)

What is the LightBD.Value actually controlling?
Is it a PointLight or something similar?

Also instead of adding or subtracting 1 to the value just make it a BoolValue, so you would set it to true or false.

1 Like

Your code is editing the value of “LightBD” you are changing the variable “value”, so nothing is going to happen.

You are creating a copy of the lightbd value where you want a reference. Copies are made by base types (numbers, strings, booleans) where references are instances and tables. The fix is as simple as dropping the .Value (number) from your variable

local lightbd = game.Workspace.Values.LightBD --  Instance gives reference

script.Parent.Parent.ClickDetector.MouseClick:Connect(function(player)
	if lightbd.Value == 0 then
		on:Play()
		lightbd.Value += 1
	elseif lightbd.Value == 1 then
		off:Play()
		lightbd.Value -= 1
	end
end)

The second problem is that you are checking two ifs in a row that will both be true. Use an elseif to skip the second part.

So like instead of “value” being a reference to “LightBD.Value”, it just starts out as 1 or whatever the default value is.

This finally made it work, thanks for the fix! It’s always formatting problems that get me

You’re right, Bool would have been simpler if I knew it existed… but might as well not risk messing up my system now. Next time though, I’ll remember!

1 Like