Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

2 comments

1

Getpropertychangedsignal.

1

you can use "ex.Changed:Connect()" to tell when the value has changed, then check if it meets the requirements

2 answers

✓ Accepted answer

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.