How would you return with out breaking a loop?

Hi, I’m tryna return a string that indicates if the item is equipped or not but it breaks the loop down - here is with out returning:


here is with returning:


image

as you can see it stopped looping and do the work.

how would I achieve this?

2 Likes

I don’t get how you expect return to work when it is in a for loop?

2 Likes

I knew it’s not gonna work, how would I resolve this?

2 Likes

The thing though is, I can’t do it outside the loop it self.

2 Likes

You cannot run code in the same function after returning. This is an XY problem. Please describe the original problem instead of asking help for something you think is the solution; Running code after returning is not a solution to your original problem.

5 Likes

Certainly. I know what the problem is, I just don’t know how the achieve what I’m trying to do.

2 Likes

Make a variable outside the loop and set that to “Equipped”, “Unequipped”.

And return that variable after the end of the outside of the loop. Replace return statements inside the loop with break.

2 Likes

If you are trying to print all items and then return one of them, you can save the item you want to return in a variable (that has been declared outside the loop), and return the item after the loop has finished. Example:

local item

for i, v in pairs({10, 20, 30}) do
    print(v)

    if v == 20 then
        item = v
    end
end

return item
2 Likes

I’m trying to return a string. I’ll try doing what @StrategicPlayZ said first.

1 Like