Value changing inside a for loop

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?

You can search a table with table.find

local isFound = table.find(result, "someOtherVariable")
if isFound then
    print("found")
else
    print("not found")
end
4 Likes

I forgot to say, that “result” is a dictionary, and table.find is not working on dictionaries as far as I know

2 Likes

What @MayorGnarwhal proposed is probably your best option! If you wanna go with a loop however:

local found = false
for s, r in pairs(result) do
    if r == "someOtherVariable" then
	    found = true
        break
    end
end

print(found and "found" or "not found")

*In this case, there is no need for an else because the “found” value is initialized as false.

2 Likes

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?

You never said anything about this being a local script nor about this being for some gui related stuff… Please clarify your problem again.

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.

Thank you very much for explaining, I didnt know that values inside a loop are only changing in their environment

Just to clarify: you are not using a dictionary. You are using an array filled with dictionaries!

local dictionary = {
    name  = "hello",
    name2 = "hello2"; 
}

local arrayOfDictionaries = {
    {name  = "hello", name2 = "hello2";},
    {name  = "hello", name2 = "hello2";};
}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.