Greetings fellow developers!
What I’m about to show you is a script that you can simply paste into the command bar and it will do the job of deleting unnecessary duplicate parts for you. It happens very often when we duplicate a part and then the part never gets used, or you just accidently press ctrl+d and it leaves two parts of the same type on top of each other. And when you play your game, your character starts moving 1ft every minute because you have all these unnecessary parts all over the place causing your game to lag. This script has been tested A LOT, so don’t worry about your baseplate disappearing or all of your terrain going missing. But if for some reason something goes wrong, please leave a reply explaining the issue you are facing so I can fix the problem. If your game freezes, don’t worry. It is a very common thing for it to do. If your game is really big then it might take a couple of minutes until it unfreezes. Just keep your hands off your device and you should be good to go. And without further ado, here is the script:
local ChangeHistory = game:GetService("ChangeHistoryService")
local amountDeleted = 0
local Check = Instance.new("IntValue")
Check.Name = "Check"
warn("KEEP HANDS AWAY FROM MOUSE AND KEYBOARD. YOU WILL RECEIVE A MESSAGE WITH THE AMOUNT OF OBJECTS DELETED WHEN THE PROCESS IS DONE.")
task.wait(0.4)
local function destroyDuplicate()
amountDeleted = 0
for a, b in pairs(game.Workspace:GetDescendants()) do
local success, errormessage = pcall(function()
if b:IsA("BasePart") then
Check.Parent = b
end
end)
for c, d in pairs(game.Workspace:GetDescendants()) do
if d:IsA("BasePart") and Check.Parent ~= d and d.CanCollide == true then
local success, errormessage = pcall(function()
if d.Position == b.Position and d.Size == b.Size and d.Orientation == b.Orientation and d.Name == b.Name and d.CastShadow == b.CastShadow and d.Transparency == b.Transparency and d.Material == b.Material and #d:GetTouchingParts() ~= 0 and #b:GetTouchingParts() ~= 0 then
if #d.Parent:GetChildren() == 1 then
d.Parent.Parent = nil
amountDeleted = amountDeleted + 2
else
d.Parent = nil
amountDeleted = amountDeleted + 1
end
end
end)
elseif d:IsA("BasePart") and Check.Parent ~= d and d.CanCollide == false then
local success, errormessage = pcall(function()
local OverPars1 = OverlapParams.new()
OverPars1.FilterType = Enum.RaycastFilterType.Whitelist
OverPars1.FilterDescendantsInstances = {d}
local OverPars2 = OverlapParams.new()
OverPars2.FilterType = Enum.RaycastFilterType.Whitelist
OverPars2.FilterDescendantsInstances = {b}
if d.Position == b.Position and d.Size == b.Size and d.Orientation == b.Orientation and d.Name == b.Name and d.CastShadow == b.CastShadow and d.Transparency == b.Transparency and d.Material == b.Material and #game.Workspace:GetPartBoundsInBox(d.CFrame, d.Size, OverPars1) ~= 0 and #game.Workspace:GetPartBoundsInBox(b.CFrame, b.Size, OverPars2) ~= 0 then
if #d.Parent:GetChildren() == 1 then
d.Parent.Parent = nil
amountDeleted = amountDeleted + 2
else
d.Parent = nil
amountDeleted = amountDeleted + 1
end
end
end)
end
end
end
if amountDeleted == 1 then
Check.Parent = nil
print(amountDeleted.." Object was removed...")
else
Check.Parent = nil
print(amountDeleted.." Objects were removed...")
end
end
destroyDuplicate()
ChangeHistory:SetWaypoint("Delete Duplicate Objects")
When it is done doing its job, it will print the amount of objects it deleted. I tested this on a game I am currently working on, and it was able to delete 304 duplicate objects including meshes, parts, models, etc.
If you are still confused and don’t know what I’m talking about, it’s alright. Here’s a super short video presentation of what the script does: Short Video Presentation
As you can see, the script deleted the other part which was on top and printed the amount of objects that were removed.
If you are still a little hesitant and don’t think this script will fully work, here’s a Roblox Studio file where you can test the code yourself and see some results: DeleteDuplicatesScript - Presentation.rbxl (110.8 KB)
If you want to save the code in your computer so you don’t have to be going back and forth to this post, here’s a .txt
file you can download: DeleteDuplicatesScript - Paste In Command Bar.txt (2.5 KB)
If something goes wrong, and you want to undo the changes, don’t panic. If you press the undo button everything should go back to normal.
And with that said, do whatever you want with the script. Oh, and also, thank you for taking time out of your day to read about my hopefully helpful for you creation.