Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there something wrong in this Boolean script?

Asked by 10 years ago

Is there something wrong with this script? Studio crashes every time I run a test with this script I made:

01e1 = script.Parent.EmergencyLight1
02e2 = script.Parent.EmergencyLight2
03e3 = script.Parent.EmergencyLight3
04e4 = script.Parent.EmergencyLight4
05e5 = script.Parent.EmergencyLight5
06es = script.Parent.EmergencyLightSc
07val = script.Parent.Value.Value
08 
09while true do
10    if val == true then
11        e1.SpotLight.Enabled = true
12        e1.SpotLight2.Enabled = true
13        e2.SpotLight.Enabled = true
14        e2.SpotLight2.Enabled = true
15        e3.SpotLight.Enabled = true
View all 39 lines...

2 answers

Log in to vote
4
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

duckwit is correct: infinite loops with no waits crash ROBLOX. You also have to reference the ValueObject, not its current Value.

Additionally, there are two things that make this a lot smaller and more efficient:

Since you only want to update the values when the Value changes, you can use the Changed Event of ValueObjects. If you'll notice, when the val is true, you set everything to true, and vice versa. You can simply set everything to the value of val for ease:

01e1 = script.Parent.EmergencyLight1
02e2 = script.Parent.EmergencyLight2
03e3 = script.Parent.EmergencyLight3
04e4 = script.Parent.EmergencyLight4
05e5 = script.Parent.EmergencyLight5
06es = script.Parent.EmergencyLightSc
07val = script.Parent.Value
08 
09val.Changed:connect(function()
10    e1.SpotLight.Enabled = val.Value
11    e1.SpotLight2.Enabled = val.Value
12    e2.SpotLight.Enabled = val.Value
13    e2.SpotLight2.Enabled = val.Value
14    e3.SpotLight.Enabled = val.Value
15    e3.SpotLight2.Enabled = val.Value
View all 23 lines...
Ad
Log in to vote
3
Answered by
duckwit 1404 Moderation Voter
10 years ago

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.

1while true do
2--Your code here
3end

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:

1while true do
2    wait()
3    if val == true then
4        --Do stuff with your lights
5    end
6end

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:

1while wait() do
2    if val == true then
3        --do stuff with your lights
4    end
5end

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:

1val = 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:

1switch = script.Parent.LightSwitch

Then in your loop, check the Value property of switch like-so:

1if switch.Value == true then
2    --Do things
3end

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:

1switch = script.Parent.LightSwitch
2 
3while wait() do
4    if switch.Value then
5        --Lights on
6    else
7        --Lights off
8    end
9end

Make sure to read the blog article about using wait() Edit: The ultimate condition should be switch.Value, not just switch

1
You have a typo in your final code block. Perci1 4988 — 10y
0
Thanks! duckwit 1404 — 10y

Answer this question