Why isn't my health refreshing when joining?

I made this script which calcutes my health and refreshes it upon join, but when I join, I have the default 100 Health. Any idea?

I also tried putting a wait to give it some time to load the character but it still doesn’t work


Script Source

	player.CharacterAdded:Connect(function(char)
		
		local hum = char:WaitForChild("Humanoid")
		
		hum.MaxHealth = StarterHealth + (Level.Value * HealthIncrementPerLevel) + (health.Value * hpPerPoint)
		hum.Health = StarterHealth + (Level.Value * HealthIncrementPerLevel) + (health.Value * hpPerPoint)
		
		maxStamina.Value = starterMaxStamina + (maximumStamina.Value * maxStaminaPerPoint)
		stamina.Value = starterMaxStamina + (maximumStamina.Value * maxStaminaPerPoint)
	end)

A long time ago I made a code that saves and loads life, maybe the script can help you:

local DataStore = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local HealthData = DataStore:GetDataStore("HealthData")

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local Humanoid = char:WaitForChild("Humanoid")
		
		Humanoid.Died:Connect(function()
			local Get_Data_Died = {
				Health = char.Humanoid.MaxHealth;
				MaxHealth = char.Humanoid.MaxHealth;
			}
			local Success, err = pcall(function()
				HealthData:SetAsync(plr.UserId, Get_Data_Died)
			end)
		end)	
		
		local Get_Data = nil
		local Success, err = pcall(function()
			Get_Data = HealthData:GetAsync(plr.UserId)
		end)
		if Success and Get_Data ~= nil then
			if Get_Data.Health >=  Get_Data.MaxHealth then
				Humanoid.MaxHealth = Get_Data.MaxHealth
				Humanoid.Health = Get_Data.MaxHealth
			else
				Humanoid.MaxHealth = Get_Data.MaxHealth
				Humanoid.Health = Get_Data.Health
			end
		else
			Humanoid.MaxHealth = 100
			Humanoid.Health = 100
		end
	end)
	plr.CharacterRemoving:Connect(function(char)
		local Get_Data_Removing = {
			Health = char.Humanoid.Health;
			MaxHealth = char.Humanoid.MaxHealth;
		}
		local Success, err = pcall(function()
			HealthData:SetAsync(plr.UserId, Get_Data_Removing)
		end)
		if Success then
			print("Se ha guardado correctamente / Has been saved successfully")
		else
			print("hubo un error al guardar / there was an error saving")
			warn(err)
		end
	end)
end)
1 Like

I’m currently on vacation. Thanks for helping, i will try this script as soon as i get back home.