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

How do I check if a condition has happened/changed?

Asked by 4 years ago
local ex = game.Workspace.example --BoolValue

function PrintSomething()
    print("Worked")
end



while wait() do
    if ex.Value == true then
        PrintSomething()
    end
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?

1
Getpropertychangedsignal. JesseSong 3916 — 4y
1
you can use "ex.Changed:Connect()" to tell when the value has changed, then check if it meets the requirements jediplocoon 877 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago

You can use the .Changed and :GetPropertyChangedSignal(" ") events.

local ex = game.Workspace.example -- bool

-- example 1
ex.Changed:Connect(function()
    --do stuff
end)


--example 2
ex:GetPropertyChangedSignal("Value"):Connect(function()
    -- do stuff
end)

Read about GetPropertyChangedSignal ~ Read about Changed

Ad
Log in to vote
1
Answered by 4 years ago

You could do it like this which will automatically do the function when a property of the bool value has changed

local ex = game.Workspace.example --BoolValue

ex.Changed:Connect(function()
    print("Worked")
end

while wait() do
    if ex.Value == true then
        PrintSomething()
    end
end

Also your loop is wrong, it should be done like this

while true do
    wait() -- add this wait since some loops might crash and not work, this helps with that
end

If you want it to print only when it's true use a normal function with a loop, so something like this

local ex = game.Workspace.example

local function PrintSomething()
    print("Worked")
end

while true do
    wait()
    if ex.Value == true then
        PrintSomething()
    end
end

Answer this question