Hello, Im currently working on a SCP game and was about to script a room with a button to turn the light on/off. But i have the problem that the lights wont change their Enabled/Disabled state.
This is because you have assigned light
to the value of LightState.Value
, which could be true
or false
at the time when you’re getting it. In order to get the “correct” value, you would have to index the “Value” property again (doing LightState.Value
to see).
“Bad” code:
local isLightOn = LightState.Value;
-- LightState.Value changes
if isLightOn then
print("Light is on!");
end
The problem with this is that isLightOn
will stay the same until you change it again.
Correct code for this use:
local lightState = LightState;
-- LightState.Value changes
if lightState.Value == true then
print("Light is on!");
end
Now, this should fix the issue you’re having.
Well, the value itself works and the parts are changing texture. The problem are the lights who wont deactivate.
Your code will work as it is , though not very efficient
To make it work you need to connect the OnClick() Function first to an event
Just add this line on the top
script.Parent.MouseButton1Click:Connect(OnClick())
if your script.Parent is not a gui button then you will need to change the code.
if it is a clickdetector then add this instead
script.Parent.MouseClick:Connect(OnClick())
assuming script.Parent here is a ClickDetector.
I tried to explain it above, but my apologies if I made myself unclear, that was not intentional! Just as stated above, you have retrieved the values of these lights in the variables. So oneOne
is now just a boolean (either true or false), and does nothing. It doesn’t change the value like you want it to do.
That’s why you have to make the script reference to the actual objects. So instead of doing:
local oneOne = ...SurfaceLight.Enabled
you should do
local oneOne = ...SurfaceLight
and then use the following code to change its value
oneOne.Enabled = true
To better demonstrate how it works:
local surfaceLight = workspace.SurfaceLight;
local surfaceLightEnabled = surfaceLight.Enabled;
-- // Example 1:
-- Check if it's enabled
print(surfaceLightEnabled) --> true
-- Make it not enabled
surfaceLight.Enabled = false
-- Check if it's still enabled
print(surfaceLightEnabled) --> true
--------------------
-- // Example 2:
print(surfaceLight.Enabled) --> true
print(surfaceLightEnabled) --> true
-- Here you *want* to change the surfaceLight Enabled property
surfaceLightEnabled = false
-- The results:
print(surfaceLight.Enabled) --> true
print(surfaceLightEnabled) --> false
without actually connecting/binding OnClick to an event you cant run a function
please tell us whether this is a gui button or a clickdetector so we can help you!!!
- and the sooner the better
thanks
You can’t reference the value and change it. When you do local value = Value.Value
then do value = false
it doesn’t change it, because it’s referencing a boolean which doesn’t allow you to change it.
local value = script.Parent
...
value.Value = true
If it is a gui button that is responsible to switch on and off lights
I recommend creating a folder containing all the surface lights (you can re-position them)
use this code parented to the button :
local all_lights = workspace.Folder:GetChildren()
for i,light in pairs(all_lights) do
if light:IsA("SurfaceLight")then
script.Parent.MouseButton1Click:Connect(function()
if light.Enabled==true then
light.Enabled=false
elseif light.Enabled==false
then light.Enabled=true
end
end)
First off, your function will never be called. You need to connect the function to a MouseClick
event of the ClickDetector.
Secondly, your boolvalues’ value will always remain the same. What you’re doing is creating a variable and setting it to its value.
thanks for repeating whatever i’ve said a few posts earlier , but i’m not sure if it is a clickdetector or a gui button. If we were provided just
some
information then @Flauschi_XD we will be able to help you a million times better
Thx for the help, its working now!
Iam using a clickdetector and the issue was not the detector.