Script only works once

Greetings,

Recently I’ve been working on a script to remove someone’s clothes and accessories and paint their body parts like these are their clothing. Well, the script runs once perfectly fine, but once the player dies and it tries to run again, it finds a “different?” character, since it only contains the player’s body parts and humanoid when in reality the player’s character has clothing, values, etc. I figured this out by printing out all descendants of the character after it dies.

Anyone can tell me what’s up? Am I passing an old character and so it’s not working?

local function SetUp(player)
		
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	game.Workspace:WaitForChild(player.Name)	
	
	repeat wait() until humanoid:GetState() ~= Enum.HumanoidStateType.Dead
	
	if character:FindFirstChild("Band") then character.Band:Destroy() end
	
	GiveBand(player)
	humanoid.Died:Connect(function() print(humanoid.Parent.Name) SetUp(game.Players[humanoid.Parent.Name]) end)

	for i,v in pairs(character:GetChildren()) do

		if v:IsA("Accessory") or v:IsA("CharacterMesh") then
			v:Destroy()
		end
		
	end
	
	for i,v in pairs(character:GetChildren()) do
		print(v.Name)
	end
	
	if character:FindFirstChild("Pants") then print("found pants") character.Pants:Destroy() end
	if character:FindFirstChild("Shirt") then print("found shirt") character.Shirt:Destroy() end
	if character:FindFirstChildWhichIsA("ShirtGraphic") then character:FindFirstChildWhichIsA("ShirtGraphic"):Destroy() end
	
	character.ChildAdded:Connect(function(object)
		if object:IsA("Pants") or object:IsA("Shirt") or object:IsA("ShirtGraphic") then
			object:Destroy()
		end
	end)
	
	character["Left Leg"].BrickColor = BrickColor.new("Medium stone grey")
	character["Right Leg"].BrickColor = BrickColor.new("Medium stone grey")
	character["Torso"].BrickColor =  BrickColor.new("Medium stone grey")

	if player.Team then
		
		character["Left Leg"].BrickColor = player.Team.TeamColor
		character["Right Leg"].BrickColor = player.Team.TeamColor
		character["Torso"].BrickColor =  player.Team.TeamColor
		
	end
	
end

players.PlayerAdded:Connect(SetUp)

No errors, by the way, just that problem.

Output when player joins: Screenshot by Lightshot
Output after player dies once: Screenshot by Lightshot

Shouldn’t it be in the player and not when the player joins?

1 Like

On this line right here, I made sure that if the player dies, it’ll run the function again with the player who died.

You probably want to run the function when a character has been added. Instead of firing it when the player joins? Maybe try this:

game.Players.PlayerAdded:Connect(function(Player)
   Player.CharacterAdded:Connect(SetUp)
end)

This does the same thing but saves lines.

Try doing when the character added…

It does but im pretty sure it does not run when the character has been added.

If the character has already been added before the function ran, which is what I understood from your question, it’ll still work.

The problem is not that it’s not finding a character, but that it’s not finding the right character.

Elaborate, cause im getting at that it doesnt run a second time for the right character…correct?

The script runs whenever the player dies, which is what I’m looking for.

The problem is that, it’s not finding the correct character, if you look at the end of my post, I provide two screenshots of what the output finds when the player joins, and when the player dies once. Regardless of how much time I set a wait() to, after the character dies it just finds some parts that a normal character would have, but not all of them. It doesn’t find my character’s pants or shirt even thought I’m wearing it.

I know this is a very complicated question to understand from an outside perspective but I’m trying my best to elaborate.

Not sure if this helps, but this is the difference between what’s found when the player first joins in and what’s found after the player dies and we wait for his character to load back in.

Even though it says there’s no shirt or pants, my character is wearing both shirt and pants.

When a player joins a game it makes the function “SetUp” function or run. If you want to make that run multiple times for each player joining than that should work. If you want to re-run that function you would write function SetUp()

If you want the script to rerun when a player dies you should do a Player.Character.Health:GetPropertyChangedSignal(“Value”)… I’m not sure if health is in the character… hope it helps :slight_smile:

I’m not quite sure what you understood from my question but that’s exactly what I’m doing.

humanoid.Died:Connect(function() SetUp(player) end)

Function SetUp() just requires an argument, which is the player instance.

It would just be :Connect(SetUp)

It wouldn’t because SetUp() requires a player instance as an argument.

local function SetUp(player)

Is yours not functioning??? Just curious

If you read the post, it says the script is working for the first time, second time it finds a different character with different descendants, which is not what I’m looking after.

I wanted to see if anyone has also had this problem using Humanoid.Died in the past.

I’ll try to figure this out by myself for now then.

Sorry I couldn’t help but it could be because the function is when the Character is nil because it has been killed maybe add a wait()