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