So I am trying to make an inventory system, and have made all the slots which are currently just empty frames numbered. I have code where I made the Slots as Keys and have a boolean as the value. So basically an example: "Slot1 = false" Slot 1 being the key and false being the Value. I made a for loop using ROBLOX wiki that iterated through all the Keys and prints the values. Now, I just want it to stop as soon as a value becomes True, but I have no idea how to do that. Any help appreciated, thank you.
COMMUNITY QUESTION
How do you make a For loop iterate through a dictionary table until a value is true?
1 answer
for i,v in next, tab do
if v == true then break end
end
The code above loops through the table 'tab', 'i' is the placing in the table, it might equal a number or a string. 'v' is the value of that placing in the table, it can equal anything. As the loop goes through the table it runs a 'if statement' checking if the value 'v' equals true, if it does the loop breaks, aka it stops.
This discussion is preserved as it was. New answers, comments, and votes are closed.

9 comments
for i, value in pairs(table) do if type(value) == "boolean" then break end
Or is that not what you're looking for?
im pretty sure he wants to check if the boolean is true
So like I have a table and some slots are true some are false, I basically want to keep checking until the value is true, and then be able to call that slot
for i, value in pairs(table) do if type(value) == "boolean" then if value == true then table[value]?
Yea, I think that work. I'm currently reorganzing my code n stuff so I can try new stuff and debug easier. So if it is true, then would I do like a break? I've never really used Breaks in loops so I'm not completely sure how to implement it. And also, "if value == true then", will I be able to pull the key(i in this case) from that somehow?
It runs a loop through the dictionary, so when that value pops up you can just put it in another table.
Okay thank you very much! I don't think I can mark your comment as answered though because it's not it the answer question box.
Alright! It worked! I did what you said and then "if value == true then --do what i want here-- break". I haven't used for loops much in my time learning Lua, so I had no knowledge of the "break" thing. Thank you!!! I have been stuck on this for a while!