Why does Studio crash?
The reason that Studio crashes is because you have, by what you have written, instructed the computer to endlessly devote all of its resources to checking the truth of a boolean value and then setting the properties of various lights.
That is the purpose of a while
loop, to continue checking the condition and then executing the contents until either the computer fails, the boolean condition is broken, or until the loop yields.
You want the loop to yield - that means to surrender its current processes for a time interval, allowing the computer to handle different tasks instead of this one loop.
The most straightforward way to yield any process is to make a call to the wait()
function. You should have the loop wait()
a little bit each time through the loop, for example:
You should see the description of wait()
on the Official ROBLOX Wiki and also read this blog article about using wait()
According to the Lua 5.1 Reference Manual, anything that is not false
or nil
will evaluate to true
in a boolean expression, and because of that, many people use this 'shorthand' notation when writing while
loops:
Value Instances
We'd need to know more about what script.Parent.Value.Value
is to properly answer this, but I'm guessing that it's referring to the Value
property of an Instance
that is a child of script.Parent
and is called "Value"
. If this is the case, I recommend naming it something more descriptive, for clarity, such as "LightSwitch" or "EmergencyToggle".
That line would then become:
1 | val = script.Parent.LightSwitch.Value |
There is another slight problem here, though. The line above stores the current boolean value of the LightSwitch
BoolValue
Instance
in a variable, it does not keep a reference to the Instance
itself. What this means is that when LightSwitch.Value
changes, val
will not change. To avoid this problem, you'll want to store a reference to the BoolValue
Instance
itself:
1 | switch = script.Parent.LightSwitch |
Then in your loop, check the Value
property of switch
like-so:
1 | if switch.Value = = true then |
Tip
switch.Value
is a boolean value; it has a value of either true
or false
. Therefore, for ease of writing, you don't need to use the expression if switch.Value == true
, instead you can just write if switch.Value
.
Putting it all together, you now have this loop to work with:
1 | switch = script.Parent.LightSwitch |
Make sure to read the blog article about using wait()
Edit: The ultimate condition should be switch.Value, not just switch