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)