MaxHealth for stats system, not saving

i give up trying to figure out how to do this for 2 days, had to seek for help right here.

what i am trying to achieve? adding max health to player every time a stats is added with the health bar details changing,

what is my problem? the health bar, health bar text, and humanoid.MaxHealth not saving after the player died

the following video is me using ResetOnSpawn screengui

and this one is ResetOnSpawn set to false

tried few things and they dont seem to work

Maybe try a loop to see the player’s current health? What I see is that you are setting the health? But how about checking for it?
Or you can set the bar to full when the player respawns

1 Like

Player’s character gets cloned again after dying. So this may be why it is not changed. Maybe try something inside the server script that will get the abilities and multiply the value with them.

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)

        local humanoid = char:WaitForChild("Humanoid")

	    local strength = "" -- Define the location
	    local agility = "" -- Define the location
	    local vitality = "" -- Define the location
	    local strength = "" -- Define the location

        humanoid.MaxHealth = 100 + (vitality.Value * 15)   -- Example
        humanoid.Health = humanoid.MaxHealth
    end)
end)

Set the ResetOnSpawn property to false.

2 Likes

Simply create a datastore saved IntValue called “MaxHealth”

Then when you upgrade the player MaxHealth, you change the IntValue and not the Character humanoid MaxHealth

To avoid to create much infinity loop scripts, there is a default script added by roblox called “Health” it is auto created in your character if there is no other script already called “Health”

So enter in play mode, copy this health script in your character, disable the play mode and past the script in the “StarterCharacterScripts”.

Here is the default code

-- Gradually regenerates the Humanoid's Health over time.

local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.

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

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

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

while true do
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
	end
	Humanoid.HealthChanged:Wait()
end

Here is how you can edit it

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Player = game.Players:FindFirstChild(Character.Name)
local MaxHealth = Player:FindFirstChild("MaxHealth")

local REGEN_RATE = 1/100 -- 0/100 if you want to disable auto heal
local REGEN_STEP = 1


while true do
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
	end
	if MaxHealth ~= nil and not Humanoid.MaxHealth == MaxHealth.Value  then 
		Humanoid.MaxHealth = MaxHealth.Value
	end
	Humanoid.HealthChanged:Wait()
end
2 Likes

You will have to set the player’s stats every time they respawn using the Player.CharacterAdded event there is no other way of going about it.

2 Likes

You may place a Script in StarterCharacterScripts.
I’ll suppose that you’re using a IntValue placed inside player.

local char = script.Parent
local hum = char.Humanoid
local plr = game.Players:GetPlayerFromCharacter(char)
local Vitality = plr.Vitality

wait(1)

AdditionalHealthValue = Vitality.Value * 15 
hum.MaxHealth = AdditinalHealthValue + 100
hum.Health = hum.MaxHealth

This will run everytime a character respawn, so we don’t need functions or loops.

2 Likes

I assume you already have a datastore set up for the health, all you really need to do is this

game:GetService("Players").PlayerAdded:Connect(function(Player)
	
	local HealthDataStore = -- Whatever the object of your maxhealth data store is in

	
	Player.CharacterAdded:Connect(function(Character)
Character.Humanoid.MaxHealth = HealthDataStore.Value
Character.Humanoid.Health = HealthDataStore.Value
end)
end)

1 Like

Did some big brain moves and came up with a conclusion with using both @ProBaturay and @FuturisticGP 's scripts, thanks for other who tried to help