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

How to use datastores that saves the players total leaderstat value?

Asked by 6 years ago

So for this game, I've made the players leaderstat value reset to 0 each time they join.

I have this script that saves their leaderstat value when they leave :

local Data = game:GetService("DataStoreService"):GetDataStore("StageDataStore")

local function Added(Player)
    local LS = Instance.new("Folder",Player)
    LS.Name = "leaderstats"
    local Stage = Instance.new("NumberValue", LS)
    Stage.Name = "Stage"
end

game.Players.PlayerRemoving:Connect(function(Player)
    Data:SetAsync(Player.UserId, Player.leaderstats.Stage.Value)
end)

game.Players.PlayerAdded:Connect(Added)

How can I add the players existing leaderstat to the datastore since all of it will be reset once they rejoin? Or in other terms how can I save the players total, overall leaderstat values?

I believe that I have to use UpdateAsync but I couldn't figure it out. Help please!

0
Did you check studio access to api services? (under configure this game) hellmatic 1523 — 6y
0
Yea, the script works fine for what it does but I need to change the script itself Terpsichora 4 — 6y

1 answer

Log in to vote
0
Answered by
Azuc 112
6 years ago

From what you asked I think you are trying to say that you want them to start from 0 each time they join but still have their overall kills for each time they have played be saved somewhere. If you are just looking for a general datastore thats fine as well this will fith both needs.

Create a folder in ReplicatedStorage called PlayerData, update whatever pvp scripts you are using to update the Kills and Deaths inside that folder alongside your current leaderstats via the hierarchy of:

game.Players = game.Players.LocalPlayer
game:GetService("ReplicatedStorage").PlayerData:WaitForChild(player.name).Stats

You can then have a place in game or on a gui to display their total career stats by pulling the values from the folder referenced above. The main script below simply goes in a script in the ServerScriptService.

I added a cash line if you have a money system you would like to save as well but you can simply remove it if it is not needed as well as copying those lines for more stats you wish to create.

Don't forget to change the key on the first line as well.

local DS = game:GetService("DataStoreService"):GetDataStore("PUTRANDOMKEYHERE")
local PlayerData = game:GetService("ReplicatedStorage"):WaitForChild("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
    local PlayerFolder = Instance.new("Folder", PlayerData)
    PlayerFolder.Name = player.name

    local Stats = Instance.new("Folder", PlayerFolder)
    Stats.Name = "Stats"

    local Kills = Instance.new("IntValue", Stats)
    Kills.Name = "Kills"

    local Deaths = Instance.new("IntValue", Stats)
    Deaths.Name = "Deaths"

    local Cash = Instance.new("IntValue", Stats)
    Cash.Name = "Cash"

    local SavedData = DS:GetAsync(player.userId)

    if SavedData then --// Load Saved Data
        for i,v in pairs(SavedData) do
            if 1 % 2 == 0 then
                PlayerData[player.Name].Stats:FindFirstChild(SavedData[i - 1]).Value = v
            end
        end
    end
end)

local function SaveValues(player) --// Save Data Function
    local Data = {}
    for i,v in pairs(PlayerData[player.name].Stats:GetChildren()) do
        Data[#Data + 1] = v.Name
        Data[#Data + 1] = v.Value
    end
    DS:SetAsync(player.userId, Data)
end

game:BindToClose(function() --// Save Data on server close
    for i, player in pairs(game.Players:GetPlayers()) do
        SaveValues(player)
    end
end)

game.Players.PlayerRemoving:connect(function(player) --// Save Data on player leaving
    SaveValues(player)
end)
0
thank you for the beautiful response, It wasn't for kills however i managed to figure it out thanks to your script :) Terpsichora 4 — 6y
Ad

Answer this question