just as the title says! can this be done if you guys could help me i would appreciate it
What do you mean by “return stuff”? If you mean with a function, just check if v is what you want with an if statement and do return v as you would with any other return.
function isOne()
local table = {}
for i, v in pairs(table) do
if v == 1 then
return v
end
end
end
Just store them on a table.
local Storage = {}
for I, V in pairs(Table) do
table.insert(Storage, I, V)
end
oh that’s a good idea i think i will do that
so you cant use the return keyword for it though right?
Wouldn’t this just duplicate the existing table…? I’m not entirely sure what this accomplishes.
The return keyword will return the current value just like @ComplicatedParadigm said. If you want to, You can just declare a variable and check inside the loop if the value given is what you want to, Then make the previous declared variable the current value and break the loop.
local MyValue = nil
for I, V in pairs(Table) do
if V == 1 then
MyValue = V
break
end
end
I am aware that table.insert exists i just don’t come around to using it very often after seeing this i think ill use it more
for index, value in pairs(game.Workspace:GetChildren())
return value
end
This just returns the first item in the table. If OP just wants the first item in a table then can just index it. It doesn’t make sense to use a for loop for this …
EX: To get the integer 3, in this table, we just use the [] operator.
local table = {3,4,5}
print(table[1])
i understand how to reference things in a table but thank you
FWIW: you can also manually iterate whatever table you have if you want to “return” a pair from a for loop. Return will terminate a loop within the iteration it’s called in and for loops aren’t callbacks so return isn’t actually going to give you anything back if you use it in a for loop alone, you need a closure.
local myChildren = something:GetChildren()
local index, child = next(myChildren)
while child ~= nil do
myChildren[index] = nil
-- Do something with index and child
index, child = next(myChildren)
end
??? you can put local on for i,v is this Lua? did not know that
… I’ve got what i need i prefer if you guys keep from commenting on the post
local dict = {["a"] = true, ["b"] = false}
for i, v in pairs(dict) do
print(i, v)
end
The variables “i” and “v” here are implicitly declared locally.