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

How to make a Data store for MaxHealth/Health?

Asked by
fr2013 88
6 years ago
Edited 6 years ago

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:

1function savedata(dataname, playerid, value)
2    if InStudio then return end
3    dataname:SetAsync(playerid, value)
4end

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

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

And Health change when player levels up

1player.Character.Humanoid.MaxHealth = levelz.Value * 50 + 250 --Change 100 to the health value
2player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth --Sets player health to max
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 User#19524 175 — 6y
0
But how do I make a data store for health. Like do I make Humanoid.MaxHealth or something? fr2013 88 — 6y

1 answer

Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
6 years ago
01local HealthData = game:GetService("DataStoreService"):GetDataStore("PlayerHealthData")
02local DataKey = "~~" -- ..UserId
03 
04 
05-- Load The Health
06function loadHealth(Character)
07    local Humanoid = Character:WaitForChild("Humanoid")
08    local PlayerHealthData = HealthData:GetAsync(DataKey..player.UserId) -- Players Data In The Data Store
09    if PlayerHealthData then -- Checks If Player Has Data
10        Humanoid.MaxHealth = PlayerHealthData[1]  -- Sets Max Health To Stored Health Value In The DataStore
11        Humanoid.Health = Humanoid.MaxHealth -- Sets same as MaxHealth
12    else -- If Player Does Not Have Data
13        Humanoid.MaxHealth = 100 - Set Default Health
14        Humanoid.Health = Humanoid.MaxHealth -- Sets same as MaxHealth
15    end
View all 48 lines...

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

0
Thanks I'll trying out soon and see if it works fr2013 88 — 6y
0
So do i implement this into my Leaderboard script or create a new script in ServerScriptService and add it in there? fr2013 88 — 6y
0
What ever one you wanna do. LuaDLL 253 — 6y
Ad

Answer this question