I’m trying to make my leaderboard save my data but i don’t understand https://developer.roblox.com/articles/Saving-Player-Data
so can someone help me or make a script for me?
here is my code:
local players = game:GetService(“Players”)
local leaderboardData = game:GetService(“DataStoreService”):GetDataStore(“LeaderStats”)
local button = script.Parent
game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new(“Model”, player)
leader.Name = “leaderstats”
local cash = Instance.new(“IntValue”, leader)
cash.Name = “Cash”
local crimelevel = Instance.new(“IntValue”, leader)
crimelevel.Name = “Crime Level”
crimelevel.Value = 0
end)
the code is located in SeverScriptService.
You can use mine if you want.
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "Something" ) |
02 | -- In Something put a text like 'Coins' 'CoinSave' etc. it is up to you. |
03 | game.Players.PlayerAdded:Connect( function (player) |
04 |
05 | local key = "Coins_ID:" ..player.UserId |
06 |
07 | local folder = Instance.new( "Folder" ,player) |
08 |
09 | folder.Name = "leaderstats" |
10 |
11 | local coins = Instance.new( "IntValue" ,folder) |
12 |
13 | coins.Name = "Coins" |
14 |
15 | coins.Value = 0 |
16 |
17 | local save = DataStore:GetAsync(key) |
18 |
19 | if save then |
20 |
21 | coins.Value = save |
22 |
23 | end |
24 |
25 | repeat |
26 |
27 | DataStore:SetAsync(key,coins.Value) |
28 |
29 | wait( 5 ) |
30 |
31 | until game.Players.PlayerRemoving |
32 |
33 | game.Players.PlayerRemoving:Connect( function () |
34 |
35 | DataStore:SetAsync(key,coins.Value) |
36 |
37 | end ) |
38 |
39 | end ) |
I use this and it will create a leaderboard, and save it every 5 seconds![and when a player is leaving]