Destroy Function not Working

I used a print statement after the Shirt:Destroy() to make sure it was running and it runs every time, but the actual shirt is only deleted 2 times before it ceases to work. I am not getting any errors in the output screen.

function SetPants(Bottom)
	if Dummy:FindFirstChild("Pants") then
		Dummy.Pants:Destroy()
		print("Pants Destroyed")
	else
		print("NoPants")
	end

	local NewPants = Bottom:Clone()
	NewPants.Parent = Dummy

	PantsCooldown = false
end
1 Like

I dont see Shirt:Destroy() any where in your script. Is this your complete script or?

1 Like

My bad I meant Pants:Destroy() but the same thing is happening with my shirt function too.

1 Like

Still can I get the whole script please so I can test it it may help in finding the error

1 Like

Try this:

for _, v in pairs(Dummy:GetChildren()) do
   if v.Name == "Pants" then
   v:Destroy()
end
2 Likes

I can’t give it to you, it is tied up with a bunch of stuff in workspace.

1 Like

weird I just tested the code you gave us and it works

Maybe try what @FeaturedLua said

also update what she said to

for _, v in pairs(Dummy:GetChildren()) do
   if v.Name == "Pants" then
      v:Destroy()
   end
end
3 Likes

I tried and got the same result, if you can I opened up the test place so I could show you the bug - t - Roblox

1 Like

What do you mean by it ceases to work after two times? My first guess would be that the new clones you are making don’t have the same name “Pants”, therefore the FindFirstChild won’t find them. However, you said that it’s still printing “Pants Destroyed”?

1 Like

Yes so what I am making is a loadout system so whenever you press the button it changes to the next pair of pants. It fires that function every time and says it is deleted too but it will only switch to the next pair twice then it will stay at that same pair of pants. The name is correct every time so I do not understand why it just stops.

1 Like

I have actually just noticed it doesn’t change the text of the current item as well so it might be something in this portion but I can’t even begin to guess what.

PantsButton.MouseButton1Click:Connect(function()
		if not PantsCooldown then
			PantsCooldown = true

			local PantsText = PantsButton.Text

			if Plr.Team.Name == "Civilian" then
				if PantsText == "Blue Jeans - 1" then
					PantsText = "Blue Jeans - 2"
					SetPants(BlueJeans2)

				elseif PantsText == "Blue Jeans - 2" then
					PantsText = "Blue Jeans - 3"
					SetPants(BlueJeans3)

				elseif PantsText == "Blue Jeans - 3" then
					PantsText = "Grey Jeans"
					SetPants(GreyJeans)

				elseif PantsText == "Grey Jeans" then
					PantsText = "Jean Shorts"
					SetPants(JeanShorts)
				elseif PantsText == "Jean Shorts" then
					PantsText = "Blue Jeans - 1"
					SetPants(BlueJeans1)
				end
			elseif Plr.Team.Name == "Military" then
				-- Military Clothes
				PantsText = "Army Combats"
				SetPants(CombatsBottom)
				-- Military Clothes
			end
		end
	end)
1 Like

I would open the file and test for you but unfortunately I cannot at the moment, so I am sorry for that. I believe that it might be you not changing the name to Pants, and the function searching for the name “Pants” before destroying the object. There are multiple ways you can go about fixing this (if this is the problem)

  1. You can change the name of the pants after instancing them, so that the next time the function is called it will find those pants. You would put this after the new pants are cloned, in your SetPants() function:

NewPants.Name = “Pants”

OR

  1. Instead of searching by the name, you could search by the Instance’s class type. So you could search for the pants using
    local oldPants = Dummy:FindFirstChildWhichIsA(“Accessory”)
    or whichever type of class your pants are.
    if oldPants then oldPants:Destroy() end

And I would hope this fixes your problem

1 Like

Fixed it, it was not trying to name the proper thing which threw off the sequence.

1 Like