Can someone help me make this weld cleanup function shorter/look cleaner?

The script works but when you look at it, its uhhh… :neutral_face:

function Weld:CleanUp(player)
	local character = player.Character
	local holsterClone = character.Holster:GetChildren()
	for i, v in pairs(holsterClone) do
		v:Destroy()
	end
	local primaryClone = character.Primary:GetChildren()
	for i, v in pairs(primaryClone) do
		v:Destroy()
	end
	for i, v in pairs(character:GetDescendants()) do
		if v:IsA("Motor6D") and v.Name == "MainHandle" then
			v:Destroy()
		end
	end
	for i, v in pairs(character:GetDescendants()) do
		if v:IsA("Weld") and v.Name == "MainHandle" then
			v:Destroy()
		end
	end
	print("worked")
end

Basically destroys the children of primary and holster folders
image

function Weld:CleanUp(player)
    local character = player.Character or player.CharacterAdded:Wait()

    character.Holster:ClearAllChildren()
    character.Primary:ClearAllChildren()

    for _, Child in pairs(character:GetDescendants()) do
        if (Child:IsA("Motor6D") or Child:IsA("Weld")) and Child.Name == "MainHandle" then
            Child:Destroy()
        end
    end

    print("worked")
end

try out this code, tell me if you got any problems

1 Like

Here yuh go, I fixed it since someone was getting mad

function Weld:CleanUp(player) --// Your function
	local character = player.Character
	local success, errorMessage = pcall(function() --// Incase a error code doesent STOP
		character.Holster:ClearAllChildren() --// Clears EVERYTHING in it
		character.Primary:ClearAllChildren() --// Clears EVERYTHING in it
		for _, object in ipairs(character:GetDescendants()) do --// Gets EVERYTHING in player character
			if (object:IsA("Weld") or object:IsA("Motor6D")) and object.Name == "MainHandle" then --// Check if its the thing your looking for
				object:Destroy() --// Deletes weld
			end
		end
	end)
	if success then --// If there were no problems deleting the welds and motor6Ds
		print("Worked!")
	else --// If there was a problem
		print("Something Went Wrong!")
		warn(errorMessage)
	end
end

two problems with your code

  1. remember and v.Name == "MainHandle", yea you never checked for the name in that if statement
  2. you never removed the folder’s children(Holster and Primary)

They can just add that in their self? They just asked for it shortened so I did a bit for them.

but it does work, I just dident put it all in.

I fixed it are you happy noww?

if statement is better, but still broken
try this

if (object:IsA("Weld") or object:IsA("Motor6D")) and object.Name == "MainHandle" then

other then that, your code should work now

why would I need the extra parentheses? (For Learning Purposes)

the code inside your if statement would run if the object is a weld or it will run if the object is both a Motor6D and is named MainHandle

by adding the parenthesis you make it so it can’t just run if the object is a Weld, it would also have to be named MainHandle to run

sorry if this was a bit confusing to read

EDIT:

if a and c or b and c then is the same as if (a or b) and c then

1 Like

Thank you two for the response :sweat_smile:, I never knew Instance:ClearAllChildren() existed.

1 Like

FYI both Motor6D and Weld inherit from JointInstance, so instead of a line of IsA checks just do one on JointInstance. Motor6D and Weld IsA JointInstance therefore is a candidate for deletion.

Here’s one you can use:

function Weld:CleanUp(player)
    local character = player.Character
    if not character then return end

    for _, child in ipairs(character:GetChildren()) do
        if child:IsA("Model") and (child.Name == "Holster" or child.Name == "Primary") then
            child:ClearAllChildren()
        elseif child:IsA("JointInstance") and child.Name == "MainHandle" then
            child:Destroy()
        end
    end
end
1 Like