Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

HP resets when the player dies?

Asked by 2 years ago
Edited 2 years ago

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 is 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 once the player dies & levels up!

1 answer

Log in to vote
0
Answered by 2 years ago
local Players=game:GetService"Players"
workspace.ChildAdded:Connect(function(v)
local player=Players:GetPlayerFromCharacter(v.Parent)
if player then
 wait()local value,hum=player.leaderstats.Level.Value,v.Parent.Humanoid
hum.MaxHealth=math.floor(100 + (value ^ 2.5 + value * 40))
 hum.Health=hum.MaxHealth
end
end)
Ad

Answer this question