Script only runs once until reset/respawn

So I have a screen gui that has a “remove pants” and “remove shirt” button. When pressed the pants button puts the player’s original pants on and the the shirt button gives the player their original shirt. I just save the players shirt and pants when they join the game and use the saved id’s to put them back on the player.

Those work fine, the issue is I have outfits players can wear throughout the map. These are all accessed in a script with a click detector in workspace. These outfits are all accessory attachments (Ie: they have handles).

For a shirt, (same w/the pants) the first time the shirt is clicked, it destroys the players ClassName == Shirt then destroys any other bodyfrontattachments, then clones the handle of the new shirt and adds the attachment to the player.

If I click on the reset shirt button, it returns the player’s original shirt but then when I try to click on any other clothes, it gives the attachment but does not remove the ClassName == Shirt anymore.

This is the part of the script to remove the ClassName Shirt. The script is in the clothing the player clicks to try on. It uses a click detector to fire


	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	for _, v in pairs(char:GetChildren()) do
		if v:IsA("Shirt") then
			if v.ClassName == "Shirt" and v:IsA("Shirt") then
				v:Destroy()
			end
		end
	end

If I reset either by respawning or using LoadCharacter the script will once again run but only until I reset again.

I have tried for weeks to figure this out and hope someone can help on this. Thanks in advance.

Edited to add a video of what’s happening. Sorry if I didn’t explain it well.

1 Like
if v:IsA("Shirt") then
	if v.ClassName == "Shirt" and v:IsA("Shirt") then
		v:Destroy()
	end
end

What is the purpose of the extra if statement? The first one checks if v is a shirt, then the second checks v again?

I have removed the 1st “if” statement on almost script except of course the script I copied and pasted :sweat_smile: I had written that a while ago when I had no understanding of i,v in pairs.

1 Like

Ah ok. Also, is the code snippet from the post found in the clothing items or is it part of the Reset Shirt button?

It’s in the clothing items in a regular script.

So the script gets removed when you switch clothing items right? That must be why the reset shirt button doesn’t work.

I recommend doing a script separate from any clothing items that handles the reset features.

so here is the complete script in each clothing item. When I reset either the pants or shirt via the gui, I can still add the clothing accessory attachments from this script, it just doesn’t remove the original shirt anymore

local button = script.Parent.Button
local clicker = button.ClickDetector
clicker.MaxActivationDistance = 25
local item = script.Parent.Item


local function giveAccessory(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	for _, v in pairs(char:GetChildren()) do
		if v:IsA("Shirt") then
			if v.ClassName == "Shirt" and v:IsA("Shirt") then
				v:Destroy()
			end
		end
	end
	
	for key, obj in pairs(char:GetChildren()) do
		if obj:IsA("Accessory") then
			local accessory = obj
			local handle = obj:FindFirstChild("Handle")
			for i,v in pairs(handle:GetChildren()) do
				if v.Name == "BodyFrontAttachment" and v:IsA("Attachment") then
					accessory:Destroy()
				end
			end
		end
	end
	
	local clonedItem = item:Clone()
	clonedItem.Handle.Anchored = false
	hum: AddAccessory(clonedItem)
end

clicker.MouseClick:Connect(giveAccessory)

make sure all changes to shirts and accessories are being done on the server, not the client. otherwise when you try to remove or add a shirt, it won’t work.

Try deleting the second if statement after if v:IsA(“Shirt”). I doubt that it’s the issue, but worth trying.

Here is the script to reset the shirt from the gui. This is probably the issue. I had a difficult time getting this to work, and I am sure there are errors in this.

This is from a localscript under startergui

shirtButton.MouseButton1Click:Connect(function()
local currentshirt = player:FindFirstChild("OGShirt")
	local char = player.Character or player.CharacterAdded:Wait()
	local Humanoid = game.Workspace:FindFirstChild(player.Name):FindFirstChild("Humanoid")

	if Humanoid and Humanoid.Parent:FindFirstChild("Shirt") == nil then
		local p = Instance.new("Shirt", Humanoid.Parent) p.Name = "Shirt"
		end
	if Humanoid.Parent:FindFirstChild("Shirt") then
		Humanoid.Parent.Shirt.ShirtTemplate = currentshirt.Value
	
	end
end)

this is the serverscript in sss

local Cooldown = false
local OriginalStored = false
game.Players.PlayerAdded:Connect(function(player)
	local OGshirt = Instance.new("StringValue", player) OGshirt.Name = "OGShirt"
	local OGpants = Instance.new("StringValue", player) OGpants.Name = "OGPants"
	player.CharacterAdded:Connect(function(char)
		wait(1)
		local Human = char:FindFirstChild("Humanoid")
		if Human and Human.Parent:FindFirstChild("Shirt") == nil then
			return
		end

		if Human and Human.Parent:FindFirstChild("Pants") == nil then
			return
		end
		if Human and Human.Parent:FindFirstChild("Shirt") then
			OGshirt.Value = Human.Parent.Shirt.ShirtTemplate  -- the value for the players shirt
			print(OGshirt.Value)
			end
		if Human and Human.Parent:FindFirstChild("Pants")  then
			OGpants.Value = Human.Parent.Pants.PantsTemplate	-- value for player's pants
			print(OGpants.Value)	
			Human.Parent.Shirt.ShirtTemplate = OGshirt.Value
			Human.Parent.Pants.PantsTemplate = OGpants.Value
			Cooldown = true
			OriginalStored = true			--bool that determines that IDs are stored as varibles
			wait(2)
			Cooldown = false
		end
	end)
end)

and I actually did remove the additional if statement for the shirts and it didn’t help unfortunately.

1 Like

Do some print statements for the localscript. Print 1 if the first if statement runs and 2 if the second if statement runs. Try recreating the issue you’re having with the reset button not working and see what it prints.

Wanted to add, I have a “reset” button that uses LoadCharacter to reset the player to their original clothes. This works perfectly after I click it, the script to remove the shirt from the players works. But it resets both the Shirt and the Pants and I was trying to do that as individual things.

Is there a way to use LoadCharacter for individual objects, like a Shirt apart from Pants.? That seems so much easier, but Idk if that exists.

And @ZurichBT the reset button will work each time, and will put the original shirt back on the player. It’s the script in the clothing items that is suppose to remove the Shirt that doesn’t. But the part of the same script to remove attachments works, it’s just the part to remove ClassName == Shirt that doesn’t. No errors in the output either. Thanks for trying to help. I have been at this for weeks.

LoadCharacter does what is says, so if anything is a child of the character, it will get LoadCharacter’d. Sadly, you can’t do it for individual items.

Also, it would be interesting to see how far the script gets in the ClassName == Shirt part. Try putting print statements at each step and see how far it gets. That may help find the issue.

So I did as you suggested in the clothing script. It removes the ClassName on the first run through, and now I don’t know if this is significant or not, but since I am wearing classic clothes, I don’t have any relevant bodyattachment to remove. So there is nothing to destroy in that part of the script and of course nothing printed.

When I try and add the clothes the second time, nothing prints, so even though it’s replacing the attachments properly, it’s not actually destroying the attachments I have put on. Does that make sense?

Here’s the script with the print statements and the output

local button = script.Parent.Button
local clicker = button.ClickDetector
clicker.MaxActivationDistance = 25
local item = script.Parent.Item


local function giveAccessory(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	for _, v in pairs(char:GetChildren()) do
		if v.ClassName == "Shirt" and v:IsA("Shirt") then
			print ("Shirt Found")
			v:Destroy()
			print ("Shirt Removed")
				

	for key, obj in pairs(char:GetChildren()) do
		if obj:IsA("Accessory") then
			local accessory = obj
			local handle = obj:FindFirstChild("Handle")
			for i,v in pairs(handle:GetChildren()) do
				if v.Name == "BodyFrontAttachment" and v:IsA("Attachment") then
					accessory:Destroy()
					print ("Attachment Removed")
				end
			end
		end
	end
		end
	end
	
	local clonedItem = item:Clone()
	clonedItem.Handle.Anchored = false
	hum: AddAccessory(clonedItem)
end

clicker.MouseClick:Connect(giveAccessory)

14:03:47.770 Shirt Found - Server - giveAcc:12
14:03:47.770 Shirt Removed - Server - giveAcc:14

Quick note-- you got something weird going on with the end of your two functions. I don’t even know how you got the script to run!

Instead of doing 2 print statements surrounding v:Destroy(), do a single print statement after each condition is met. For example:

if true then
     print(1)
     if false then
         print(2)
     end
end

Hi you can detect when the player dies and remove the shirt and pants I believe!

local char = player.Character or player.CharacterAdded:Wait()

function removeClothing()
local hum = char:WaitForChild("Humanoid")
for _, v in pairs(char:GetChildren()) do
		if v:IsA("Shirt") then
			if v.ClassName == "Shirt" and v:IsA("Shirt") then
				v:Destroy()
			end
		end
	end
end

removeClothing()

char:WaitForChild("Humanoid").Died:Connect(function()
char = player.CharacterAdded:Wait()
removeClothing()
end)

The thing is I am not trying to kill the player. Just remove their shirt (or pants) which works the first time I run it, but won’t work again until I kill the player.

This wont kill the player, it will detect when they die/reset. Then remove it again.

So it prints 1 and 3 the first time I do it, and nothing after even when it still changes my attachments. Did I use the print statements correctly? Thanks again for your help!

if v.ClassName == "Shirt" and v:IsA("Shirt") then
			if true then
				print(1)
				if false then
					print(2)
				end
				end
			v:Destroy()
					if true then
						print(3)
						if false then
							print(4)
						end
					end
					
			
			for key, obj in pairs(char:GetChildren()) do
				if obj:IsA("Accessory") then
					local accessory = obj
					local handle = obj:FindFirstChild("Handle")
					for i,v in pairs(handle:GetChildren()) do
						if v.Name == "BodyFrontAttachment" and v:IsA("Attachment") then
									if true then
										print(5)
										if false then
											print(6)
										end
										end
							accessory:Destroy()
											if true then
												print(7)
												if false then
													print(8)
												end
											end

14:15:48.176 Shirt Found - Server - giveAcc:12
14:15:48.176 Shirt Removed - Server - giveAcc:14