01 | local ex = game.Workspace.example --BoolValue |
02 |
03 | function PrintSomething() |
04 | print ( "Worked" ) |
05 | end |
06 |
07 |
08 |
09 | while wait() do |
10 | if ex.Value = = true then |
11 | PrintSomething() |
12 | end |
13 | end |
Problem
Right now I'm trying to figure out the most efficient way to properly check if a condition has changed or a better way to say it is if a certain requirement has been meant for code to run. The code above isn't supposed to be anything complicated; I made it to find different ways to check for changes, but the way I did it is really the only way I've really understood how to do it.
And it doesn't really work. So, is there different methods in checking for conditions?
Question
How can I check for certain conditions/requirements and have code run in response?
You can use the .Changed
and :GetPropertyChangedSignal(" ")
events.
01 | local ex = game.Workspace.example -- bool |
02 |
03 | -- example 1 |
04 | ex.Changed:Connect( function () |
05 | --do stuff |
06 | end ) |
07 |
08 |
09 | --example 2 |
10 | ex:GetPropertyChangedSignal( "Value" ):Connect( function () |
11 | -- do stuff |
12 | end ) |
You could do it like this which will automatically do the function when a property of the bool value has changed
01 | local ex = game.Workspace.example --BoolValue |
02 |
03 | ex.Changed:Connect( function () |
04 | print ( "Worked" ) |
05 | end |
06 |
07 | while wait() do |
08 | if ex.Value = = true then |
09 | PrintSomething() |
10 | end |
11 | end |
Also your loop is wrong, it should be done like this
1 | while true do |
2 | wait() -- add this wait since some loops might crash and not work, this helps with that |
3 | end |
If you want it to print only when it's true use a normal function with a loop, so something like this
01 | local ex = game.Workspace.example |
02 |
03 | local function PrintSomething() |
04 | print ( "Worked" ) |
05 | end |
06 |
07 | while true do |
08 | wait() |
09 | if ex.Value = = true then |
10 | PrintSomething() |
11 | end |
12 | end |