Respawning with Equipped Character Bug

I have a skin shop system in my game, and just recently I’ve been working on getting it so that when a player respawns, they’ll respawn as their equipped skin. I got it to sem-work, but for some reason, it repeats 6 times. So when you respawn, it’ll respawn you as the equipped skin, but will also create 5 clones AND leaves your default character there from when you initially spawned (before it equipped the skin).

I tried added a wait() to stop the if statement from running again, and it worked, but every time the wait would run out, it would spawn 1 clone after every wait until it reached 5 clones again, HOWEVER with this, it did stop the original default character from being left there.

I also tried adding a debounce, but I’m unexperienced so I might not’ve done it right. It pretty much just did the exact thing it did without the debounce, even when I didn’t define a time for the debounce to re-enable.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char) 
		local Folder = plr.CharacterShop.Equipped 
			print(#Folder:GetChildren()) 

		if #Folder:GetChildren() == 0 then
			print("Folder is empty")
		else
			local newCharacter = plr.CharacterShop.Equipped:FindFirstChild("savedCharacter"):Clone()
			newCharacter.Name = plr.Name
			plr.Character = newCharacter
			newCharacter.Parent = workspace
			print("Success!")
		end

Any clue what might be causing this? I can provide more of the script if you need.

1 Like

Every time you assign newCharacter to the player’s character, it fires the .CharacterAdded event. That is why it is repeating. Consider getting the player’s character using plr.Character over the .CharacterAdded event.

2 Likes

CharacterAdded, based off its documentation, fires every time player.Character is set to a non-nil value. In essence, this is going to cause your code to constantly loop. I couldn’t tell you why it stops at specifically 5 clones, though.

Couple of ways to fix this. One is to listen to the CharacterRemoving event to assign the new Character instead of listening to CharacterAdded.

Alternatively, you could add an attribute to the newCharacter that identifies it as a skin. Then, when CharacterAdded fires, you can check char to see if it’s a skin, and if so, stop code execution.

2 Likes

I tried using the CharacterRemoving instead of CharacterAdded but this didn’t seem to work, so I opted for your idea of using attributes; however, I’m not experienced with attributes so I might’ve messed up.

The issue seems to persist where it spawns 5 clones. I’ve found that is specifically 5 because there is a limit to how many times you can run this particular event, according to the output.

Here’s my revised code:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char) 
		local Folder = plr.CharacterShop.Equipped 
 			print(#Folder:GetChildren()) 

		if #Folder:GetChildren() == 0 then
			print("Folder is empty")
			
		else 
			if not plr.Character:GetAttribute("IsSkin") then

				local newCharacter = plr.CharacterShop.Equipped:FindFirstChild("savedCharacter"):Clone()
				newCharacter.Name = plr.Name
				plr.Character = newCharacter
				newCharacter.Parent = workspace
				print("Success!")
				newCharacter:SetAttribute("IsSkin", true)

			end
		end