Problem:
The script tends to set the parent of the object to the parent that it was being set to even though my script tries to delete it. It seems that Roblox does not like something to remove or destroy something before the actual parent of it is placed within that object or whatever.
What I want:
I want the script to find what object is being added to a part in the player's model and to destroy it if it is under the destroy list. The problem is, the script will print the line where it is deleted but Roblox will not allow the object to be deleted.
Extra:
If you guys think that my code could be done more efficiently I would love to know how also. Mostly, I am not looking for a complete rewrite of my code but rather tips to further improve my code and possible ways I could go about this to prevent this from happening.
Code:
local model = script.Parent
local persn = script.Parent
local exceptions = {"Climbing", "Died", "FreeFalling", "GettingUp", "Jumping", "Landing", "Running", "Splash", "Swimming", "face", "FaceCenterAttachment", "FaceFrontAttachment", "HairAttachment", "HatAttachment", "RootJoint", "RootAttachment", "LeftGripAttachment", "LeftShoulderAttachment", "LeftFootAttachment", "RightGripAttachment", "RightShoulderAttachment", "RightFootAttachment", "Left Hip", "Left Shoulder", "Neck", "Right Hip", "Right Shoulder", "BodyBackAttachment", "BodyFrontAttachment", "LeftCollarAttachment", "NeckAttachment", "RightCollarAttachment", "WaistBackAttachment", "WaistCenterAttachment", "WaistFrontAttachment", "roblox"}
local delete25 = {"Part", "Folder", "Sound", "Accoutrement", "Accessory", "AlignOrientation", "AlignPosition", "Attachment", "BallSocketConstraint", "Beam", "ClickDetector" , "CylindricalConstraint" , "Explosion" , "Fire", "HingeConstraint", "LineForce", "SpecialMesh", "Mesh", "ParticleEmitter", "PointLight", "PrismaticConstraint", "RodConstraint", "RopeConstraint", "Script", "Smoke", "Sparkles", "SpringConstraint", "Tool", "Torque", "Trail", "VectorForce", "Decal", "Function", "LocalScript", "Texture", "ModuleScript", "RemoteEvent", "BillboardGui", "BodyAngularVelocity", "BodyForce", "BodyGyro", "BodyThrust", "BodyVelocity", "BodyPosition", "RocketPropulsion", "ScreenGui", "SurfaceGui", "SelectionBox", "Animation", "AnimationController", "Configuration", "Dialog", "BoolValue", "BrickColorValue", "CFrameValue", "Color3Value", "IntValue", "NumberValue", "StringValue", "Vector3Value"}
function sortexp(name, clas, object, part)
print(name)
print(clas)
print(part)
print(object)
if exceptions[name] then
print("You Are clear")
else
sortdele(name, clas, object, part)
print("You are not clear")
end
end
function sortdele(Namea, Clas, Object, Part)
print("Arrived")
for i,v in pairs(Part:GetChildren()) do
if v.Name == Namea then
print("getting deleted")
v:Destroy()
print("It's gone")
if v.Name == Namea then
v:Destroy()
end
end
end
end
script.Parent:WaitForChild("HumanoidRootPart").ChildAdded:Connect(function(descda) --This is just one of the things I will be using to check, other parts such as the Right Arm and the Torso will also have this event too. That is why I decided to use functions and tables in order to stop as much repetition as possible.
print("Something was added, lets check")
sortexp(descda.Name, descda.ClassName, descda, script.Parent:WaitForChild("HumanoidRootPart"))
end)
What I get in the output:
16:20:17.575 - Something unexpectedly tried to set the parent of Value to NULL while trying to set the parent of Value. Current parent is Players.
Like I said, any type of feedback would be nice. And thanks for giving up some of your time to help me ;3.

2 comments
I am mostly trying to devoid from using a while loop as I have some already and I don't want to lag the game down more, because more than one person will have this script and the other components also.
Why? It will only run like 1 or 2 times.