Script only runs once until reset/respawn

Okay, so there are some noticeable differences. The most obvious one is that print(2) is never printed in the output on the second attempt, which proves that the script isn’t able to destroy. However, it is able to get past the first print(1), which means the issue lies in the second condition. More specifically:

if v.ClassName == "Shirt" and v:IsA("Shirt") then
	print(2)
	v:Destroy()
end

To translate into English, whatever v is, it definitely isn’t a shirt. If it is, then something is up with your condition (the text between if and then)

But this is good progress. You finally found the issue!

Could you attach them both again? thanks.

Thank you that at least gives me somewhere to start.
Any idea why this would not work as written.

Not sure, but as I said at the beginning, I found it really weird that you were checking twice if v was a Shirt. I know you said you fixed that statement already, but here the problem still remains. So, try getting rid of the v.ClassName == "Shirt" and just keep v:IsA("Shirt). There is no need for the former.

local script from the screengui

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)

and the serverscriptservice script

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)
1 Like

ok so correct me if i’m wrong, but you currently are using the local script to replace the shirt right?

so I did this with the script, and nothing printed, lol But it still worked the first time I ran the script.

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
	local hum = char.Humanoid
	for _, v in pairs(char:GetChildren()) do
		print(1)
		if v:IsA("Shirt") then
			print(2)
			v:Destroy()
		end
	end

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

	local clonedItem = item:Clone()
	clonedItem.Handle.Anchored = false
	hum: AddAccessory(clonedItem)
end

clicker.MouseClick:Connect(giveAccessory)
1 Like

No, I am using a regular script in the item in workspace. This is the script.

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
	local hum = char.Humanoid
	for _, v in pairs(char:GetChildren()) do
		print(1)
		if v:IsA("Shirt") then
			print(2)
			v:Destroy()
		end
	end

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

	local clonedItem = item:Clone()
	clonedItem.Handle.Anchored = false
	hum: AddAccessory(clonedItem)
end

clicker.MouseClick:Connect(giveAccessory)

thats the script that does the resetting?

Something should print. If you mean that print(2) didn’t show up in the output, then that makes sense. But everything else should’ve printed like before.

Assuming that nothing changed with the output, that means that there are literally zero Shirts to destroy.

Try this script, which adds a print statement to show the list of objects within char:GetChildren():

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
	print(char:GetChildren())
	local hum = char.Humanoid
	for _, v in pairs(char:GetChildren()) do
		if v:IsA("Shirt") then
			v:Destroy()
		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)

no sorry, that is a localscript under screengui. It’s this

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)

Actually, try this script instead to see the ClassName of v.
This should yield more interesting results.

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
	local hum = char.Humanoid
	for _, v in pairs(char:GetChildren()) do
		print(v.ClassName)
		if v:IsA("Shirt") then
			v:Destroy()
		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)
1 Like

i believe that is the issue. you are applying changes locally on the client, which means the server cannot see these changes. This means that when the server goes to make a change from another script, it causes problems because it cant see those changes from the localscript.

The solution would be to use that reset localscript to activate a remote event and then have a normal script detect when it goes off and then apply the changes there.

Changes made in localscripts are only visible to the client, not the server or other players. for these new changes to work, they must be done on the server.

1 Like

So when I clicked the new shirt for the first time, here is the output

15:46:28.414 Part - Server - giveAcc:11
15:46:28.415 Pants - Server - giveAcc:11
15:46:28.415 :arrow_forward: MeshPart (x5) - Server - giveAcc:11
15:46:28.415 Shirt - Server - giveAcc:11
15:46:28.416 :arrow_forward: MeshPart (x6) - Server - giveAcc:11
15:46:28.417 Humanoid - Server - giveAcc:11
15:46:28.417 Script - Server - giveAcc:11
15:46:28.417 LocalScript - Server - giveAcc:11
15:46:28.417 BodyColors - Server - giveAcc:11
15:46:28.417 :arrow_forward: MeshPart (x4) - Server - giveAcc:11
15:46:28.418 :arrow_forward: Accessory (x2) - Server - giveAcc:11

nothing when I reset the shirt from the screengui prints (because I didn’t put anything in there yet, sorry)
and then when I click the shirt again this is the output

15:47:50.144 Part - Server - giveAcc:11
15:47:50.144 Pants - Server - giveAcc:11
15:47:50.144 :arrow_forward: MeshPart (x5) - Server - giveAcc:11
15:47:50.145 Accessory - Server - giveAcc:11
15:47:50.145 :arrow_forward: MeshPart (x6) - Server - giveAcc:11
15:47:50.146 Humanoid - Server - giveAcc:11
15:47:50.146 Script - Server - giveAcc:11
15:47:50.146 LocalScript - Server - giveAcc:11
15:47:50.146 BodyColors - Server - giveAcc:11
15:47:50.146 :arrow_forward: MeshPart (x4) - Server - giveAcc:11
15:47:50.147 :arrow_forward: Accessory (x2) - Server - giveAcc:11

Lol, I didn’t actually look at what printed. So looks like only line 11 of the code is working? Which is print ClassName…

So it’s not destroying any Shirts because there are no Shirts to destroy.

But I have a shirt on, a classic shirt…and it removes it the first time I run it, but it doesn’t print anything? This is crazy,

The first time you run it, there is a shirt. You can see it in the post you just sent.

The second time you run the script, you can clearly read that there is no object with the “Shirt” ClassName in char:GetChildren().

But when I hit the reset shirtbutton, it gives me back my original shirt. nothing prints there, but I have my original shirt back on. But it’s not recognizing that the second time I run the script.

its not recognizing it because you reset only on the client and are trying to recognize that with a server script. The server cannot see changes applied only on the client.

1 Like