Hi, I am having trouble with deleting multiple parts with the same name, but leaving one left. I have tried a couple of ways that I can think of, but nothing seems to be working. Can anyone help?
You could add a value inside the one part you don’t want to delete and then do a for loop through wherever your parts are parents to…deleting every part that doesn’t have that value
for i,v in pairs(workspace:GetChildren()) do -- I'm assuming your parts are parented to workspace...so change that if you'd like
if v.Name == "YourName" then
if v.YourValue then
v:Destroy()
end
end
end
This is a good idea, and I did try this but what I’m having trouble with is that I copy the parts from a script into workspace, so how would I get a value into just one of the parts?
Just to clarify, is there something specific about that one part that needs to stay in the game, or can it just be any one of the parts?
There’s two ways you could go about this but I need to confirm that first.
No there is nothing specific about it. I just need one random part to stay.
Sounds good!
So, before you move the parts to Workspace you could do a math.random script wherever the parts are located. I recommend you keep them in a folder of some kind so that if there’s another part that’s not part of the ones you move…it doesn’t get the value.
local Parts = Path.To.Parts.Holder:GetChildren()
local randomPart = Parts[math.random(1,#Parts)]
local Value = Instance.new("ObjValue")
local Value.Name = "ValueName"
Value.Parent = randomPart
-- Then move your parts to workspace
ok thanks I will try it and report back how it goes
Now that I’m thinking about it…I would like to recommend one more method…
Unless there’s a certain reason you need to move ALL the parts to Workspace…why not just use the math.random to move one?
local Parts = Path.To.Parts.Holder:GetChildren()
local randomPart = Parts[math.random(1,#Parts)]
randomPart.Parent = workspace
That does sound a lot better than moving all of them thanks!
Where did #items come from? because that has a red line under it
My bad! I just fixed that on both scripts.
Ok thanks
i hate character limit
Would have sent this earlier but the site decided to go under view mode only
Here’s another viable solution.
function ClearNumberOfChildren(parent, instanceName, RemoveAmount)
assert(typeof(parent) == "Instance", "bad argument #1 (Instance expected, got "..tostring(parent)..")")
assert(typeof(instanceName) == "string", "bad argument #2 (String expected, got "..tostring(instanceName)..")")
assert(typeof(RemoveAmount) == "number", "bad argument #3 (Number expected, got "..tostring(RemoveAmount)..")")
local Current = 0
for _, Skim in ipairs(parent:GetChildren()) do
if Skim.Name == instanceName and RemoveAmount ~= Current then
Current += 1
Skim:Destroy()
end
end
end
ClearNumberOfChildren(workspace, "Folder", 25)
If you have 26 things named “Folder” in workspace and delete 25 you’ll have your remaining one.