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.

--What i'm trying to do

local part = Instance.new("Part", game.Workspace)           --Part we will be checking to see if it exists
part.Name = "IAmAPart"                                      --The parts name

local anotherPart = Instance.new("Part", game.Workspace)    --The other part that will also have the same name
anotherPart.Name = "IAmAPart"                               --This new part is also named the same as the first part we created in the beginning of the script

for i,v in ipairs(game.Workspace:GetChildren()) do          --Iterate through all the parts in the workspace
    if v:IsA("BasePart") then
        if v.Name == part.Name then                         --Hey, these two parts have the same name!
            part.Name = "IAmAnotherPart"                    --No more name conflicts! Huzzah!
        end
    end
end

But what if something like this happens...

--What i'm trying to do

local part = Instance.new("Part", game.Workspace)           --Part we will be checking to see if it exists
part.Name = "IAmAPart"                                      --The parts name

local anotherPart = Instance.new("Part", game.Workspace)    --The other part that will also have the same name
anotherPart.Name = "IAmAPart"                               --This new part is also named the same as the first part we created in the beginning of the script

local theThirdWheel = Instance.new("Part", game.Workspace)  --Oh wait, another part?
theThirdWheel.Name = "IAmAnotherPart"                       --But its named what I want to change the first one to...

for i,v in ipairs(game.Workspace:GetChildren()) do          --Iterate through all the parts in the workspace
    if v:IsA("BasePart") then
        if v.Name == part.Name then                                 --Hey, these two parts have the same name!
            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!
            for a,b in ipairs(game.Workspace:GetChildren()) do      --So how can I have it iterate through the loops again without doing this again?
                if b.Name == part.Name then
                    --More loops blah blah
                end
            end
        end
    end
end

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:

local part = Instance.new("Part")
part.Parent = workspace
part.Name = "IAmAPart"

local anotherPart = Instance.new("Part")
anotherPart.Parent = workspace
if workspace:FindFirstChild("IAmAPart") then 
anotherPart.Name = "IAmAnotherPart"
else
anotherPart.Name = "IAmAPart"
end


0
I feel so dumb right now stepatron 103 — 6y
Ad

Answer this question