CharacterRemoving not working?

So right now im making a system that will set a players health in a string value when they leave so that when they join back into the game (game dose not close) they will get the same health. But im having trouble with CharacterRemoving since when the player resets it also fires CharacterRemoving.

This is my current code.

local plrclone
local HumanPos
local plr
local pos = CFrame.new(0,0,0)
local int
local functioning = false
local found = false
local Players = game:GetService("Players")
local setplayerpos = function(player, sop, health)
	player.Character:SetPrimaryPartCFrame(CFrame.new(sop))
	player.Character.Humanoid.Health = health
end
local function StringToVector(str)
	local args = string.split(str, ",")
	return Vector3.new(args[1], args[2], args[3])
end
local function FindBody(name)
	local health
	local bodies = game.Workspace.Bodies:GetChildren()
	for i,v in pairs(bodies) do
		if v.Name == name then
			health = v.HealthSpawn.Value
			v:Destroy()
		end
	end
	return health
end
local anchor = function(targeta)
	local parts = targeta:GetChildren()
	for i,v in pairs(parts) do
		if v:IsA("BasePart") then
			v.Anchored = true
		end
	end
end
game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player.Character 
	local ints = game.ReplicatedStorage.PlayerValues:GetChildren()
	for i,v in pairs(ints) do
		if v:IsA("StringValue") then
			if v.Name == player.Name then
				player.PlayerGui.KillGui.RespawnSet.Value = true
				local name = player.Name
				local health = FindBody(name)
				print(health)
				local str = v.Value
				local sop = StringToVector(str)
				setplayerpos(player, sop, health)
			end
		end
	end
	plr = player
	player.Character.Archivable = true
	plrclone = player.Character:Clone() 
	plrclone.Parent = game.ReplicatedStorage.ClonedPlayers
	plrclone.Name = player.Character .Name
	local anscript = script.Script:Clone()
	anscript.Parent = plrclone
	anscript.Disabled = false
	player.Character.Archivable = false
	functioning = true
	int = Instance.new("StringValue")
	int.Name = player.Character.Name
	int.Parent = game.ReplicatedStorage.PlayerValues
	local folder = Instance.new("Folder")
	folder.Parent = int
	folder.Name = "Bags"
	found = false
	local int = Instance.new("StringValue")
	int.Name = "HealthSpawn"
	int.Parent = plrclone
	found = false
	player.CharacterRemoving:Connect(function(player1)
		local health = player1.HealthCur.Value
		local workspace = game.Workspace
		local ints = game.ReplicatedStorage.PlayerValues:GetChildren()
		local Check = Players:GetPlayerFromCharacter(player1)
		print(Check)
		if not Check then
			for i,v in pairs(ints) do
				if v:IsA("StringValue") then
					if v.Name == player1.Name then
						local targeta = game.ReplicatedStorage.ClonedPlayers:FindFirstChild(v.Name, true)
						targeta.HealthSpawn.Value = health
						local str = v.Value
						local pos = StringToVector(str)
						targeta.Parent = workspace.Bodies
						targeta:SetPrimaryPartCFrame(CFrame.new(pos))
						print(targeta.HealthSpawn.Value)
						wait(5)
						anchor(targeta)
					end
				end
			end
		end
	end)
end)
Players.CharacterRemoving:Connect(function(player)
	local health = player.HealthCur.Value
	local workspace = game.Workspace
	local ints = game.ReplicatedStorage.PlayerValues:GetChildren()
	for i,v in pairs(ints) do
		if v:IsA("StringValue") then
			if v.Name == player.Name then
				local targeta = game.ReplicatedStorage.ClonedPlayers:FindFirstChild(v.Name, true)
				targeta.HealthSpawn.Value = health
				local str = v.Value
				local pos = StringToVector(str)
				targeta.Parent = workspace.Bodies
				targeta:SetPrimaryPartCFrame(CFrame.new(pos))
				print(targeta.HealthSpawn.Value)
				wait(5)
				anchor(targeta)
			end
		end
	end
end)

as you can see i tried to solve this issue by making this line of code

local Check = Players:GetPlayerFromCharacter(player1)

but the character is removed before the Player value is so even when i leave Check will = my player. How can i fix this?

Use PlayerRemoving instead. It detects when the player leaves the game.

Ive tried using PlayerRemoving but the thing is that when i do that sometimes the character will already have been destroyed so i cant get the player health.

Use:

player.PlayerRemoving:connect(function(plr)
health = plr.Character.Humanoid.Health
end)

Note: For the saving, save the health variable, and replace player with the player path.

1 Like

You should use the humanoid.Died event instead, it fires when the humanoid’s health is 0 or lower.