How do I make a function that constantly checks until a value is true? Then if the value is true it does something.
Sounds like you want a while loop. Here is a code example below:
ValueIsTrue = false
function TrueCheck()
While ValueIsTrue == false do
task.wait()
end
-- Add code here for when value is true
end
TrueCheck()
More info on while loops can be found here: Introduction to Scripting | Roblox Creator Documentation
repeat
task.wait(0.5) --you can change this on how often it checks
until ThisValue == true
--Code to run after it's true.
This would enable a script timeout and kill the script. There needs to be a task.wait()
inside the loop. This will run a million times instantly without the task.wait()
.
I see you added wait()
to your script. This should be task.wait()
because wait()
is being deprecated.
Saying this value is an instance or an attribute there are appropriate events for them, you can check on their respective page.
If this is regarding a variable, you would so something like
local RunService = game:GetService("RunService")
local Variable = False
task.spawn(function()
while not Variable do
RunService.Heartbeat:Wait() -- you can also task.wait()
end
--your code to run here
end)
Reason for spawning is not stopping the whole code when waiting for it to change.
Thanks for letting me know! I found the post about task.wait().
Shouldn’t you use a changed event instead?
OP did not specify if it was a variable or an instance value.
wouldn’t a while true loop work best?
OP didn’t specify if it was an IntValue or a variable.