Why doesn't my code work?

I’m trying to make a script that sets the players clothing and when the player dies it sets the players clothing again but the code I made doesn’t work

code:

game.Players.PlayerAdded:Connect(function(player)
	--Variables
	local shirtclone = game.ServerStorage.Clothing.Shirt:Clone()
	local pantsclone = game.ServerStorage.Clothing.Pants:Clone()
	local hatclone = game.ServerStorage.Clothing.Hat:Clone()
	-- Wait for the character to be loaded
	while not player.Character do task.wait() end
	local char = player.Character 

	if char then
		shirtclone.Parent = char
		pantsclone.Parent = char
		hatclone.Parent = char
		print("Set Clothing")
	end

	local humanoid = char:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		local newChar = player.Character
		while not newChar do task.wait() end
		local newShirt = shirtclone:Clone()
		local newPants = pantsclone:Clone()
		local newHat = hatclone:Clone()
		
		newShirt.Parent = newChar
		newPants.Parent = newChar
		newHat.Parent = newChar
		print("Set Clothing")
	end)

	player.CharacterRemoving:Connect(function(character)
		local shirt = character:FindFirstChild("Shirt")
		local pants = character:FindFirstChild("Pants")
		local hat = character:FindFirstChild("Hat")

		if shirt then shirt:Destroy() end
		if pants then pants:Destroy() end
		if hat then hat:Destroy() end
	end)
end)

First of all, the above isn’t necessary. You should be using CharacterAdded instead of detecting if the humanoid died.

1 Like
game.Players.PlayerAdded:Connect(function(player)
	--Variables
	local shirtclone = game.ServerStorage.Clothing.Shirt:Clone()
	local pantsclone = game.ServerStorage.Clothing.Pants:Clone()
	local hatclone = game.ServerStorage.Clothing.Hat:Clone()
	-- Wait for the character to be loaded
	while not player.Character do task.wait() end
	local char = player.Character 

	if char then
		shirtclone.Parent = char
		pantsclone.Parent = char
		hatclone.Parent = char
		print("Set Clothing")
	end

	local humanoid = char:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		task.wait(1)
		local newChar = player.Character
		while not newChar do task.wait() end
		local newShirt = shirtclone:Clone()
		local newPants = pantsclone:Clone()
		local newHat = hatclone:Clone()

		newShirt.Parent = newChar
		newPants.Parent = newChar
		newHat.Parent = newChar
		print("Set Clothing")
	end)
end)

boom. now what?

Now, replace Humanoid.Died:Connect(function() with Player.CharacterAdded:Connect(function(newChar), and remove this:

it works, just… the hat doesn’t clone for some reason…
image

The weird thing about CharacterAdded is that it runs after respawning once.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.