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

How to create a loop to check if a part name already exists?

Asked by
stepatron 103
6 years ago

How do you check if a part name exists already? Like how would you create a loop that would constantly run until it finds a part with the name that I want already, but then it checks again if the name I want to change the part to also exists without hardcoding a loop like 50 times. I keep trying to logic through this but then I get stumped and im back to square one.

01--What i'm trying to do
02 
03local part = Instance.new("Part", game.Workspace)           --Part we will be checking to see if it exists
04part.Name = "IAmAPart"                                      --The parts name
05 
06local anotherPart = Instance.new("Part", game.Workspace)    --The other part that will also have the same name
07anotherPart.Name = "IAmAPart"                               --This new part is also named the same as the first part we created in the beginning of the script
08 
09for i,v in ipairs(game.Workspace:GetChildren()) do          --Iterate through all the parts in the workspace
10    if v:IsA("BasePart") then
11        if v.Name == part.Name then                         --Hey, these two parts have the same name!
12            part.Name = "IAmAnotherPart"                    --No more name conflicts! Huzzah!
13        end
14    end
15end

But what if something like this happens...

01--What i'm trying to do
02 
03local part = Instance.new("Part", game.Workspace)           --Part we will be checking to see if it exists
04part.Name = "IAmAPart"                                      --The parts name
05 
06local anotherPart = Instance.new("Part", game.Workspace)    --The other part that will also have the same name
07anotherPart.Name = "IAmAPart"                               --This new part is also named the same as the first part we created in the beginning of the script
08 
09local theThirdWheel = Instance.new("Part", game.Workspace)  --Oh wait, another part?
10theThirdWheel.Name = "IAmAnotherPart"                       --But its named what I want to change the first one to...
11 
12for i,v in ipairs(game.Workspace:GetChildren()) do          --Iterate through all the parts in the workspace
13    if v:IsA("BasePart") then
14        if v.Name == part.Name then                                 --Hey, these two parts have the same name!
15            part.Name = "IAmAnotherPart"                            --This will clear up the naming conflict with the first two parts, but now the first and third have a naming conflict!
View all 23 lines...

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

You would have that script while defining the part. To elaborate:

01local part = Instance.new("Part")
02part.Parent = workspace
03part.Name = "IAmAPart"
04 
05local anotherPart = Instance.new("Part")
06anotherPart.Parent = workspace
07if workspace:FindFirstChild("IAmAPart") then
08anotherPart.Name = "IAmAnotherPart"
09else
10anotherPart.Name = "IAmAPart"
11end
0
I feel so dumb right now stepatron 103 — 6y
Ad

Answer this question