Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

1 answer

##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)

This discussion is preserved as it was. New answers, comments, and votes are closed.