So this if then statement is supposed to detect a certain value. However, I want it to do something else when the value changes.
01 | local Hit = script:waitforchild( "Random" ) |
02 |
03 | while wait() do |
04 | if Hit.Value = = true then |
05 | print ( 'Hi' ) |
06 | end |
07 | if Hit.Value = = false then |
08 | print ( 'Goodbye' ) |
09 | end |
10 | end |
I want the script to do something else once the value changes. How would I make the script do this? Can someone please help me?
Continent is correct, you should be using WaitForChild
and not waitforchild
.
To catch when an Value object is changed, use the Changed
event. You can also make that if
statement more efficient by using else
instead of a second if
!
1 | local Hit = script:WaitForChild( "Random" ) |
2 |
3 | Hit.Changed:connect( function () |
4 | if Hit.Value = = true then --the `== true` can be omitted here, since `if true == true then` --> `if true then` |
5 | print ( "Hi" ) |
6 | else --If it's not true, it must be false! |
7 | print ( "Goodbye" ) |
8 | end |
9 | end ) |
If you want to add more variables to check:
01 | local Hit = script:WaitForChild( "Random" ) |
02 |
03 | Hit.Changed:connect( function () |
04 | if Hit.Value and someOtherValue.Value then |
05 | print ( "Hi -- someOtherValue is true!" ) |
06 | elseif Hit.Value then |
07 | print ( "Goodbye -- someOtherValue is false!" ) |
08 | else |
09 | print ( "someOtherValue is unknown!" ) |
10 | end |
11 |
12 | --or, just add another `if` tree: |
13 |
14 | if someThirdValue.Value then |
15 | print ( 1 ) |
16 | else |
17 | print ( 2 ) |
18 | end |
19 | end ) |