HP resets when the player dies

Hey,

I’m creating an RPG-styled game, and I modify the player’s HP every time they level up. However, when they die, the HP resets and goes back to the default 100.

Here’s the code:

local replicatedStorage = game:GetService("ReplicatedStorage")
local hpEvent = replicatedStorage:WaitForChild("HpEvent")
local hpDeath = replicatedStorage:WaitForChild("HpDeathChange")

function changeHP(player, value)
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	-- Checks if the player isn't dead
	repeat wait() until humanoid.Health ~= 0
	repeat wait() until player.leaderstats.Level.Value == value
	print("yes")
	
	local health = 100 + (value ^ 2.5 + value * 40)
	local flooredHealth = math.floor(health)
	
	humanoid.MaxHealth = flooredHealth
	humanoid.Health = flooredHealth
end

hpEvent.OnServerEvent:Connect(changeHP)

-- I have a remote even that fires when the player dies and when the player levels up

maybe try setting a hum.Died event set the max humanoid health and humanoid health back to normal whenever the player respawns.

Didn’t work, also I experimented with ‘player.CharacterAdded’ which didn’t work too. The script runs right after the player dies, yet it won’t change the values for some reason.

I noticed it said you have it firing when the player dies, with this noted, when the server retrieves the information your local char = player.Character or player.CharacterAdded:Wait() is referencing the now dead Humanoid.

Rather than firing when the player dies on the client, check whether the players character is added. Or you could change local char = player.Character or player.CharacterAdded:Wait() into local char = player.CharacterAdded:Wait()

I ended up getting rid of the remote event that fires when the player dies, and everything seems to be defined correctly. I found out that the problem is that the script thinks that the player’s level is 0, yet the leaderstats says the level is 6.

Here is the current code:

local replicatedStorage = game:GetService("ReplicatedStorage")
local hpEvent = replicatedStorage:WaitForChild("HpEvent")
local hpDeath = replicatedStorage:WaitForChild("HpDeathChange")

function changeHP(player, value)
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	local health = 100 + (value ^ 2.5 + value * 40)
	local flooredHealth = math.floor(health)
	
	humanoid.MaxHealth = flooredHealth
	humanoid.Health = flooredHealth
	
	player.CharacterAdded:Connect(function()
		repeat wait() until player:FindFirstChild("leaderstats")
		local level = player.leaderstats.Level
		
		local char = player.Character or player.CharacterAdded:Wait()
		char.Humanoid.Health = 100 + (level.Value ^ 2.5 + level.Value * 40)
		char.Humanoid.MaxHealth = 100 + (level.Value ^ 2.5 + level.Value * 40)
	end)
end

hpEvent.OnServerEvent:Connect(changeHP)

I ended up fixing it; I was playing around and bounced a remote event from each script and it ended up working.

Here’s the code if anyone in the future has this issue

-- Server Sided Script

local replicatedStorage = game:GetService("ReplicatedStorage")
local hpEvent = replicatedStorage:WaitForChild("HpEvent")
local hpDeath = replicatedStorage:WaitForChild("HpDeathChange")

function changeHP(player, value)
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	print(value)
	local health = 100 + (value ^ 2.5 + value * 40)
	local flooredHealth = math.floor(health)
	
	humanoid.MaxHealth = flooredHealth
	humanoid.Health = flooredHealth
	
	player.CharacterAdded:Connect(function()
		repeat wait() until player:FindFirstChild("leaderstats")
		hpDeath:FireClient(player)
	end)
end

hpEvent.OnServerEvent:Connect(changeHP)

---------------

-- Local Script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local replicatedStorage = game:GetService("ReplicatedStorage")
local hpEvent = replicatedStorage:WaitForChild("HpEvent")
local hpDeath = replicatedStorage:WaitForChild("HpDeathChange")

local level = player.leaderstats.Level

while wait(0.1) do

	level.Changed:Connect(function()
		level = player.leaderstats.Level

		local value = player.leaderstats.Level.Value
		hpEvent:FireServer(value)
	end)
	
	hpDeath.OnClientEvent:Connect(function()
		wait(2)
		local value = player.leaderstats.Level.Value
		hpEvent:FireServer(value)
	end)
end
1 Like