I really dont know how to use this if you can help that would be great!
You don’t. table.foreach
is deprecated.
Use a generic for loop instead:
for k, v in pairs(dict) do
-- ...
end
for i, v in ipairs(list) do
end
There is documentation on it.
But it’s not like this info will be useful to you; as mentioned these are both deprecated so you wouldn’t/shouldn’t use this in production.
@xDeltaXen, he answered your question. Please don’t be rude to people trying to answer your questions. Both methods are deprecated, so the proper answer is to avoid using them, as already stated. The documentation clearly shows how they are to be used. You pass an array and a callback function that gets called each iteration.
table.foreach(tbl, function(val) ... end)
table.foreachi(tbl, function(val) ... end)
Again, don’t use them. Use pairs
and ipairs
instead.
Is there something in particular you do not understand so it can be clarified.
Wherever you would use a pairs
loop,
for k, v in pairs(someTable) do
-- body
end
you can replace it with a similar table.foreach
function.
table.foreach(someTable, function(k, v)
-- body
end)
A near-identical analogy can be made with ipairs
and table.foreachi
As for use cases (even though it’s deprecated), you can directly use a print
function without the loop body.
-- instead of
for k, v in pairs(someTable) do
print(k, v)
end
-- you can do
table.foreach(someTable, print)
Personally, I wouldn’t use it for anything else because of the key-value combo.
You are welcome to ask any question in #help-and-feedback:scripting-support so long as your topic complies with the following guidelines:
If you are having trouble providing specifics about what you need help with or don’t understand, we encourage you to do more personal research by reading Developer Hub documentation or by searching for threads that might already cover the material. (The last part of this community tutorial post seems to be what you’re looking for.)
Please keep in mind that threads in this category are meant to be used as resources for other developers, and not as a means to bypass personal research to get answers faster.