I have this line of code that waits until all 8 values are true, here it is:
repeat task.wait(.5) until stats1.Book1.Value == true and stats1.Book2.Value == true and stats1.Book3.Value == true and stats1.Book4.Value == true and stats1.Book5.Value == true and stats1.Book6.Value == true and stats1.Book99.Value == true and stats1.Book999.Value == true
I’ve noticed that a lot of people say that repeat wait() until or repeat task.wait() until is bad and people shouldn’t use it, are there any better alternatives to this? I’ve considered using while loops like while value == true and value2 == true do but isn’t that just the same thing as this? The line of code is in a LocalScript btw, idk if that helps or not.
So since you are checking if values are true (assuming they are in a folder) you could do something like this:
for i, v in pairs(foldername:GetChildren()) do
task.wait(0.5)
if v:IsA("BoolValue") and v == true then
print("Value is true!")
else
print("Value is false!")
end
end
But wouldn’t that loop through all the values in the folder? There’s a ton of values inside, and why do that when you can just say stats1.Value instead? There’s not that many anyways, and that doesn’t constantly check the values, you’d have to put that in a loop, which will be performance-heavy.
it will only check through values that have a true or false as a property such as a boolvalue which is why I told the script to check if its a boolvalue in the folder therefore it doesn’t check through everything in the folder unless you tell it to. if you wanted to check the values forever once they change (eg: false to true) then you would write :GetPropertyChangedSignal():
local value -- where your thing is that you want to check the value of
value:GetPropertyChangedSignal("Value"):Connect(function()
print(value.Value)
end)
If you put an if statement inside a for loop will it not check everything else or not loop through everything else, I know it doesn’t check everything else but I’m 99% certain it loops through everything inside the folder which will still create lag. Also I wanted the script to yield until all values are true and then continue the script.
So boolValue:GetPropertyChangedSigna("Value"):Wait() would wait until it’s true and then continue? But wouldn’t that mean I’d have to copy and paste that for each value? Why would I do that when I can just use repeat wait until which does the same thing and is only one line of code? I’m looking for better alternatives, if there is any. If there’s not then I’ll just continue using what I have.
Sorry I’m having a moment. This might work lol. I am not to sure what else:
local folderWithBoolValues = -- add all the boolvalues in a folder if you want to check them all
for i, v in pairs(folderWithBoolValues:GetChildren()) do
if not v.Value then
v:GetPropertyChangedSignal("Value"):Wait()
else
print("Value is true")
end
end
I believe you should instead read the part where it writes the context of the common usage; which is exactly what you’re doing.
The repeat wait() until is being used to stop code execution until flag is true. This is known as polling (colloquially, you’ll also hear it called busy waiting in circles for higher level software, although they’re not technically the same). Polling in our case is bad because we’re unnecessarily waiting for something to happen instead of just doing code once it happens.
Austin’s solution is definitely “better” in the actual code-sense, as you aren’t unnecessarily writing loops and your code would be event-driven.
You can definitely avoid repeat wait() with roughly the same amount of lines, if not just a little bit more. Pokemoncraft5290 is on-point with the “Less code ~= better code”.
It is a loop. You are looping “wait()” and is constantly checking the variable changes.
Alright, I think I meant to say it’s not a for loop instead. I’m gonna use Austin’s solution then. I just didn’t want to use his since I had a bad experience with loops, I ended up using loops a little too much and it severely damaged my game, but since I already found a way to fix that, I’ll still use his solution.
The for loop in this sense should not generally be an issue. What it is essentially doing is to only set-up the “Changed” signal. Then, the loop isn’t doing anything further than that.
I’m not too sure if Austin’s code is functional. Here’s a second alternative if needed with the same idea.
local folderWithBoolValues
local function checkValues()
for i, v in pairs(folderWithBoolValues:GetChildren()) do
if not v.Value then
-- >> At least one value is false
return false
end
end
-- >> All values are true
-- >> Call your function / whatever methods that happens after everything's true here
return true
end
for i, v in pairs(folderWithBoolValues:GetChildren()) do
if not v:IsA("BoolValue") then continue end
v.Changed:Connect(function()
checkValues()
end)
end