How to insert a boolvalue into a character after death?

Im trying to insert a boolvalue into the player’s character after death, however I cannot find a way how and was wondering if there even is a way to do this?

The following is my script:

game.Players.PlayerAdded:Connect(function(plyr)
	local char = plyr.Character or plyr.CharacterAdded
	local hum = char:WaitForChild("Humanoid")
	hum.Died:Connect(function()
		print(plyr.Name.." has died...")
		wait(2)
		plyr:LoadCharacter()
		wait(5)
		local a = Instance.new("Folder")
		local b = Instance.new("BoolValue")
		
		a.Name = "DeathStat"
		b.Name = "isDead"
		a.Parent = char
		b.Parent = a
	end)
end)

Try adding :Wait() at the end of this line:

local char = plyr.Character or plyr.CharacterAdded:Wait()

Wait, you should get character again when player dies so you code will be:

game.Players.PlayerAdded:Connect(function(plyr)
	local char = plyr.Character or plyr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	hum.Died:Connect(function()
		print(plyr.Name.." has died...")
		wait(2)
		plyr:LoadCharacter()
		wait(5)
		char = plyr.Character or plyr.CharacterAdded:Wait()
		local a = Instance.new("Folder")
		local b = Instance.new("BoolValue")
		
		a.Name = "DeathStat"
		b.Name = "isDead"
		a.Parent = char
		b.Parent = a
	end)
end)

Oh wow, I feel very stupid now, such an easy fix haha. Thank you so much, it worked!

1 Like

This will cause a memory leak if the player leaves before char = plyr.Character or plyr.CharacterAdded:Wait() executes, i.e; within seven seconds of their humanoid dying.

local Game = game
local Players = Game:GetService("Players")

local function OnPlayerAdded(Player)
	local function OnCharacterAdded(Character)
		local BoolValue = Instance.new("BoolValue")
		BoolValue.Parent = Character
		local function OnHumanoidDied()
			BoolValue.Value = true
		end
		
		local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(OnHumanoidDied)
	end
	
	Player.CharacterAdded:Connect(OnCharacterAdded)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

I’d create the ‘BoolValue’ object upon the character being added/loaded and then switch its value to true if the character’s humanoid dies.