Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make something happen at the end of a for i v but only once?

Asked by
Seyfert 90
7 years ago
for i, v in pairs(game.Workspace.Model1:GetChildren()) do
    local intValue = v:FindFirstChild("DataValuel") 
        if v.intValue.Value == 0 then
            ----do a thing
        break
        end
    end
end


As you guys can see, this is an issue because if the code iterates through a brick that does not have an IntValue in it or that has an IntValue but it is not set to 0 then the loop will end, I want to make it so that it will iterate through all of the bricks, and then upon iterating the correct one which is one that has a value of 0 it will do something. Removing the break does what I want, however then it will run more than once, which is what I do not want. Any ideas?

Maybe I create another if statement that checks local variable is ture and at the end of --do a thing I set it to false? I will try that it might work.

0
I'm lost; what're you trying to do? TheeDeathCaster 2368 — 7y
0
I want my for i v statement to check ALL bricks in my Model for ones that meet certain conditions and then to do something to ONLY one of the bricks that meet the certain conditions and then for the for loop to end. I tried using a break but if the for i v statement happens to come across a brick that does not meet the conditions itll just end the loop which is not what I want. Is that clearer? Seyfert 90 — 7y
0
Yes, it does. When looping through the item, you could check it via if some values (such as names, colours, etc). However, with your code, on line 3, you're essentially retyping line 2; you don't need to rewrite the original when you already have it defined. :P TheeDeathCaster 2368 — 7y
0
Ah that was a small typo, but see i still am running ingo thr issues described above. If the for i v loop happens to iterate on a brick that does not have an IntValue set to 0, the loop will exit because of the break which is not what I want it to do. I want it to iterate all bricks regardless if they dont have the value but upon finding one that does have a value, for it to do something to do the Seyfert 90 — 7y

2 answers

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

You can do this using a simple boolean flag:

local ran;
for i, v in pairs(game.Workspace.Model1:GetChildren()) do
    local intValue = v:FindFirstChild("DataValuel") 
        if intValue.Value == 0 and not ran then
            ----do a thing
            ran = true;
        end
        --// do other things
    end
end

This will not only check if intValue.Value is 0, but also if the variable ran is false or nil. At the end of the code within the if statement, the flag is set to true so it won't run more than once.

Hope this helped.
Read more about loops and booleans.

0
That is essentially exactly what I wanted but I am not comfortable with local ran; I will do local ran = false and see if that works as I understand that more. Thank you kind sir! Seyfert 90 — 7y
0
No problem. Pyrondon 2089 — 7y
Ad
Log in to vote
-1
Answered by
Voltoxus 248 Moderation Voter
7 years ago
for i, v in pairs(game.Workspace.Model1:GetChildren()) do
    local intValue = v:FindFirstChild("DataValuel") 
        if v.intValue.Value == 0 then
            -- For examples sake
        v.BrickColor = BrickColor.new("Bright red")
        return true 
        end
    end
end

See code above, break should work but I don't know what context your using this code in. If its a function return true should break the loop, and end the function call.

Answer this question