Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a Data store for MaxHealth/Health?

I might've asked this before but I don't know on how to make a data store for health. All I know is making data stores that are associated to leaderstats such as cash and level. I looked up online and even here but I can't seem to find anything about them that work. So here's the save data:

function savedata(dataname, playerid, value)
    if InStudio then return end
    dataname:SetAsync(playerid, value)
end

And I decided to add a starting health for the player:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        char.Humanoid.MaxHealth = 250 --Starting Health
        player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth --Sets player health to max
	end)
end)

And Health change when player levels up

player.Character.Humanoid.MaxHealth = levelz.Value * 50 + 250 --Change 100 to the health value
player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth --Sets player health to max

2 comments

0

They don't have to be for saving leaderstats, but data stores can be used to do so. Simply save their health like you would save anything else

0

But how do I make a data store for health. Like do I make Humanoid.MaxHealth or something?

1 answer

✓ Accepted answer
local HealthData = game:GetService("DataStoreService"):GetDataStore("PlayerHealthData")
local DataKey = "~~" -- ..UserId


-- Load The Health
function loadHealth(Character)
	local Humanoid = Character:WaitForChild("Humanoid")
	local PlayerHealthData = HealthData:GetAsync(DataKey..player.UserId) -- Players Data In The Data Store
	if PlayerHealthData then -- Checks If Player Has Data
		Humanoid.MaxHealth = PlayerHealthData[1]  -- Sets Max Health To Stored Health Value In The DataStore
		Humanoid.Health = Humanoid.MaxHealth -- Sets same as MaxHealth
	else -- If Player Does Not Have Data
		Humanoid.MaxHealth = 100 - Set Default Health
		Humanoid.Health = Humanoid.MaxHealth -- Sets same as MaxHealth
	end
end

-- Save The Health
function saveHealth(player)
	local Character = player.Character -- Character Of Player
	local Humanoid = Character:WaitForChild("Humanoid") -- Waits For The Humanoid To Be A Child Of The Character
	local Table = {Humanoid.MaxHealth} -- Puts Value Into A Table Used To Store The Data
	HealthData:SetAsync(DataKey..player.UserId,Table) -- Saves MaxHealth In Data Store
end

game.Players.PlayerAdded:Connect(function(player) 
	player.CharacterAdded:Connect(function(char)  -- Calls The loadHealth Function
		loadHealth(char)
		-- Save When MaxHealth Changes
		local Humanoid = char:WaitForChild("Humanoid")
		local OldMax = {}
		table.Insert(OldMax,Humanoid.MaxHealth)
		Humanoid.Changed:Connect(function() -- When Humanoid Properties Change
			if Humanoid.MaxHealth == OldMax[1] then return end -- Checks if maxhealth wasnt changed
			if Humanoid.MaxHealth ~= OldMax[1] then -- Checks if MaxHealth Was Changed
				saveHealth(player) -- Saves
				for i,v in pairs(OldMax) do -- Removes Old Value
					if v == Humanoid.MaxHealth then 
						table.remove(OldMax,i)
					end 
				end
				table.insert(OldMax,Humanoid.MaxHealth) -- Inserts New Value
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(saveHealth) -- Calls The saveHealth Function

i know someone else answered this but you still had some question about how to do it when the maxhealth changed so here it is.

ik the OldMax Table and stuff isnt very necessary but eh

3 comments

0

Thanks I'll trying out soon and see if it works

0

So do i implement this into my Leaderboard script or create a new script in ServerScriptService and add it in there?

0

What ever one you wanna do.

This discussion is preserved as it was. New answers, comments, and votes are closed.