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)
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.
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
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.