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
5 years ago
Edited 5 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:

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
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 — 5y
0
But how do I make a data store for health. Like do I make Humanoid.MaxHealth or something? fr2013 88 — 5y

1 answer

Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
5 years ago
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

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

Answer this question