Repeat loop with a specific condition

Hello,
Is there a way how to make a repeat loop that has a condition like this :

Until a child that has a particular name (Ex : “Part1” which would be in a variable as a string) is not found in an instance. (Ex : Workspace)

I am looking for a method which doesn’t require me to check if the “Part1” is in Workspace using a Boolvalue or something like that.

Example of what I am looking for:

local checkname = "Part1"
repeat
    (Process) 
until (Example : Part1 (Name of Part) is not found in workspace.)
2 Likes
local check = 'Part1'
repeat print'waiting' until not workspace:FindFirstChild(check, true)

You can pass true as the second argument if you want FindFirstChild to search recursively through the children.

2 Likes

You can also use FindFirstAncestor on the Part, for example:

local part = workspace.Model.Part
repeat
    print("Still a child of workspace!")
    wait()
until not part:FindFirstAncestor(workspace.Name)
4 Likes

Can’t you use BasePart:IsDescendantOf(Instance) in this case?

5 Likes

Hi @Wrathsong , @ElliottLMz , @return_end1 first of all ,thanks for replying!
I think I didn’t explain myself as clear as possible.

Let’s say there was an empty folder instead of workspace in this scenario and you are generating 3 different numbers from a script which uses a for loop. For the script to pick different numbers I’d have to use a repeat loop. Please note that I am also putting the number in the folder as an IntValue and making it the Name of the IntValue.
Here’s an example of the repeat loop :

repeat
    local number = math.random(1,3)
until (Number is not in folder)`

Ahh okay. You’d have to keep a table of the values that have already been used, that you don’t want to repeat.

local taken = {}
local number
repeat number = math.random(1,3) until not table.find(taken, number)
table.insert(taken, #taken + 1, number)
2 Likes

Yes that’s the solution I thought of earlier but still wanted to see whether there was an alternative to that.

Thank you once again!