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

How do i save my data When it comes to a win/lose leaderboard?

Asked by 7 years ago

I want to save my kills so whenever a player joins back, they have the right amount of wins and loses.

0
Sorry, no requests here. You need to show us the script for your problem. starlebVerse 685 — 7y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

What you need

In order to do what you want, you need to use DataStores.

  • Create your DataStore using the GetDataStore function of DataStoreService.

  • You can then save keys and values to your DataStore using the SetAsync function, and retrieve them using the GetAsync function.

  • You will have to retrieve saved values using a PlayerAdded event, and save the desired stat's Value using a PlayerRemoving event.


Code

--Creating the DataStore
local ds = game:GetService("DataStoreService"):GetDataStore("statz");

--Loading stats
game.Players.PlayerAdded:Connect(function(plr)
    local data = ds:GetAsync(plr.UserId);
    if data then
        plr:WaitForChild("Kills").Value = data.kills;
        plr:WaitForChild("Deaths").Value = data.deaths;
    end
end)

--Saving stats
game.Players.PlayerRemoving:Connect(function(plr)
    local data = {kills=plr.Kills.Value,deaths=plr.Deaths.Value};
    ds:SetAsync(plr.UserId,data);
end)
Ad

Answer this question