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

How would I save a specific Leaderstat value via DataStore?

Asked by 5 years ago
Edited 5 years ago
local PlayerJoined_DataStore =
game:GetService('DataStoreService'):GetDataStore('JoinedTimes')

game.Players.PlayerAdded:connect(function(Player)
    local Joined =
    PlayerJoined_DataStore:GetAsync(tostring(Player.UserId))
    local Stats = Instance.new('Folder',Player)
    Stats.Name = 'leaderstats'
    local JoinStat = Instance.new('NumberValue',Stats)
    JoinStat.Name = 'Joined'

if Joined ~= nil or Joined == 0 then
    JoinStat.Value = Joined
    print("Loaded stats for "..Player.Name)
    PlayerJoined_DataStore:SetAsync(tostring(Player.UserId), Joined + 1)
    --[[ I would like to GetAsync data for the player kill value.

    I have already made a leaderboard script with the value I want to save,
    the directory of the value I want to save is game.Players.PLRNAME.leaderstats.Kills.Value
    --]]
else
        PlayerJoined_DataStore:SetAsync(tonumber(Player.UserId), 1)
        warn("Failed to load stats for"..Player.Name)
    end
end)

Ignore the "local VALUE =" with an empty definition of the line, look below the line to see what I defined the VALUE to be.

1 answer

Log in to vote
0
Answered by 5 years ago

I would imagine that all you need to do is create a new data store:

local kills_ds = game:GetService('DataStoreService'):GetDataStore('Kills')
game.Players.PlayerRemoving:Connect(function(player)
    kills_ds:SetAsync(player.UserId,player.leaderstats.Kills.Value)
end
0
I will attempt this method, thanks for the submission! vislbIe 4 — 5y
0
My goal of my comment was to GetAsync, if I were to SetAsync in a different line (such as PlayerRemoved), then can I change the "SetAsync" for the loading portion to "GetAsync"? vislbIe 4 — 5y
0
Not sure I follow. Send me a message @BlueGrovyle#5735 on Discord. BlueGrovyle 278 — 5y
0
Method does not work, thanks for the effort. vislbIe 4 — 5y
View all comments (2 more)
0
I solved the issue myself, thanks for your time! vislbIe 4 — 5y
0
No problem! BlueGrovyle 278 — 5y
Ad

Answer this question