Prevent clothing from being removed when I reset

Hi.

So I made a shirt and pants button, and when you click it, it fires a remote event to equip the shirt and pants onto the player.

But when the player resets, the shirt and pants aren’t there anymore.
I was wondering if there was any way to stop this from happening.

The script:

Event13.OnServerEvent:connect(function(player, Shirt_ID, Pants_ID)
	local Character = player.Character
	local CharTable = Character:GetChildren()
	for i=1,#CharTable do
		if CharTable[i].ClassName == "Shirt" or CharTable[i].ClassName == "Pants" then
			CharTable[i]:Destroy()
		end
	end
	local NewShirt = Instance.new("Shirt", Character)
	local NewPants = Instance.new("Pants", Character)
	NewShirt.ShirtTemplate = Shirt_ID
	NewPants.PantsTemplate = Pants_ID

end)

Any help would be appreciated, thanks!

You could store those ID’s in a table, and check whenever the player dies and load them on when he respawned.

yeah I tried that but it didnt’t work for me
i accidently deleted the line of code so i cant show you

local Items = {}

You could insert into that table the ID’s of your clothing, and when the player resets, load them into his body( check some conditions:

  1. If they already have clothes on, if so - replace their ID with the ones in your table.

2)If not, then insert a shirt/pants into the character and put the IDs of the items in your table - accordingly.)

You need to store a cache of the player’s avatar’s clothing whenever they die and subsequently load that cache when their character is next added.

local players = game:GetService"Players"

local playersClothing = {}

local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		local function onHumanoidDied()
			local shirt = character:FindFirstChildOfClass"Shirt"
			local pants = character:FindFirstChildOfClass"Pants"
			local tShirt = character:FindFirstChildOfClass"ShirtGraphic"

			if shirt then
				playersClothing[player].Shirt = shirt.ShirtTemplate
			end

			if pants then
				playersClothing[player].Pants = pants.PantsTemplate
			end

			if tShirt then
				playersClothing[player].TShirt = tShirt.Graphic
			end
		end
		
		local humanoid = character:WaitForChild"Humanoid"
		humanoid.Died:Connect(onHumanoidDied)
		
		if not player:HasAppearanceLoaded() then
			player.CharacterAppearanceLoaded:Wait()
		end
		
		if playersClothing[player].Shirt then
			local shirt = character:FindFirstChildOfClass"Shirt" or Instance.new"Shirt"
			shirt.ShirtTemplate = playersClothing[player].Shirt
			shirt.Parent = character
		end

		if playersClothing[player].Pants then
			local pants = character:FindFirstChildOfClass"Pants" or Instance.new"Pants"
			pants.PantsTemplate = playersClothing[player].Pants
			pants.Parent = character
		end

		if playersClothing[player].TShirt then
			local tShirt = character:FindFirstChildOfClass"ShirtGraphic" or Instance.new"ShirtGraphic"
			tShirt.Graphic = playersClothing[player].TShirt
			tShirt.Parent = character
		end
	end
	
	player.CharacterAdded:Connect(onCharacterAdded)
	playersClothing[player] = {}
end

players.PlayerAdded:Connect(onPlayerAdded)

This implementation includes support for t-shirts as well, just make sure you’re changing the player’s character’s clothing on the server otherwise those changes won’t replicate.

Thank you so much, I really appreciate it!

No problem, here’s a quick improvement I just thought up of.

local players = game:GetService"Players"

local playersClothing = {}

local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		local function onHumanoidDied()
			local shirt = character:FindFirstChildOfClass"Shirt"
			local pants = character:FindFirstChildOfClass"Pants"
			local tShirt = character:FindFirstChildOfClass"ShirtGraphic"

			if shirt then
				playersClothing[player].Shirt = shirt.ShirtTemplate
			end

			if pants then
				playersClothing[player].Pants = pants.PantsTemplate
			end

			if tShirt then
				playersClothing[player].TShirt = tShirt.Graphic
			end
		end

		local humanoid = character:WaitForChild"Humanoid"
		humanoid.Died:Connect(onHumanoidDied)

		if not player:HasAppearanceLoaded() then
			player.CharacterAppearanceLoaded:Wait()
		end

		if playersClothing[player].Shirt then
			local shirt = character:FindFirstChildOfClass"Shirt" or Instance.new"Shirt"
			shirt.ShirtTemplate = playersClothing[player].Shirt
			shirt.Parent = character
		end

		if playersClothing[player].Pants then
			local pants = character:FindFirstChildOfClass"Pants" or Instance.new"Pants"
			pants.PantsTemplate = playersClothing[player].Pants
			pants.Parent = character
		end

		if playersClothing[player].TShirt then
			local tShirt = character:FindFirstChildOfClass"ShirtGraphic" or Instance.new"ShirtGraphic"
			tShirt.Graphic = playersClothing[player].TShirt
			tShirt.Parent = character
		end
	end

	player.CharacterAdded:Connect(onCharacterAdded)
	playersClothing[player] = {}
end

local function onPlayerRemoving(player)
	playersClothing[player] = nil
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
1 Like