Hello everyone,
Today I have a theoretic question.
Here is a loop:
for s, r in pairs(result) do
if r == "someOtherVariable" then
print("found")
else
print("not found")
end
end
It will print everytime “not found” if the if-statement fails. But what if I only want to print “not found” only one time, when all values from r have passed and it still hasnt fulfilled the if statement? I thought already about using a debounce, but that would delay the script too much. Any ideas?
Yeah I think that could work, but I have another problem:
for i, v in pairs(TextBoxes) do
local found = false
for s, r in pairs(result) do
if r == "someOtherVariable" then
found = true
end
end
end
Will changing the found value now also effect the other TextBoxes in this example or is it only changing in its thread, so only for the TextBox which name got found?
Alright, I am sorry. Its a local Script inside StarterGui
So its now going to be a bit confusing but I have a for loop for 4 timers to count down. In the beginning of them, I am checking in a dictionary what their old timer in the last server was and to check that, I posted the code above here.
Heres a bigger shortcut of the code:
--Result is a Dictionary and the result of :GetAsync in another ServerScript
for i, v in pairs(tools:GetChildren()) do --This is the loop to catch all, in this example, TextBoxes
if result ~= nil then --Check if the dictionary has content
for s, r in pairs(result) do --And here begins my problem
if r["name"] == v.Name then
print("FOUND IT")
else
print("Only print once if it is really not found")
end
end
end
end
Silly_dev’s solution is what you’d be looking for.
The reason it works is that you’re assuming you won’t find it, and then only saying you did find it if the if statement matches.
So, by the time the loop ends, you know that:
a. The flag is true, and you must have found the element, or
b. The flag is false, and the element was not found.
Then you can check one last time and print out based on that.
The logic works independently of the TextBox itself, so you can have the outer loop go through every text box and run the inside logic for each textbox. They won’t impact each other as long as you set found = false in the first line of the outer loop.
Alternatively, you can have it as a separate function which would probably make it clearer that the TextBoxes won’t impact each other. But it ends up being the same.