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
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”?
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.
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)
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)
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
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