Answered by
6 years ago Edited 6 years ago
There are a few issues with your script:
1) It is difficult to read, create separate functions for each task so it can be more organized (save and load data function).
2) The tables are unnecessary and it was hard to understand it. I suggest to create a variable for each value. It'll be more organized and efficient.
3) 'connect' is deprecated, use 'Connect' instead.
4) DO NOT set the parent in the Instance.new parameters. It's a faster way but it was proved that it can slow down the script.
01 | local Players = game:GetService( "Players" ) |
02 | local DataStores = game:GetService( "DataStoreService" ) |
03 | local PlayersDataStore = DataStores:GetDataStore( "Players" ) |
05 | local function SavePlayerData(Player) |
07 | local Leaderstats = Player:FindFirstChild( "perm" ) |
09 | local XPValue, RankValue, HeistsValue, PrestigeValue |
10 | XPValue = Leaderstats:FindFirstChild( "XP" ) |
11 | RankValue = Leaderstats:FindFirstChild( "Rank" ) |
12 | HeistsValue = Leaderstats:FindFirstChild( "Heists" ) |
13 | PrestigeValue = Leaderstats:FindFirstChild( "Prestige" ) |
14 | if XPValue and RankValue and HeistsValue and PrestigeValue then |
15 | local PlayerDS = PlayersDataStore:GetAsync(Player.UserId) |
18 | [ "XP" ] = XPValue.Value, |
19 | [ "Rank" ] = RankValue.Value, |
20 | [ "Heists" ] = HeistsValue.Value, |
21 | [ "Prestige" ] = PrestigeValue.Value |
23 | print ( "saved " .. Player.Name .. "'s leaderstats" ) |
24 | PlayerDataStore:SetAsync(Player.UserId, SavingTable) |
31 | local function LoadPlayerData(Player, XPValue, RankValue, HeistsValue, PrestigeValue) |
33 | local PlayerDS = PlayersDataStore:GetAsync(Player.UserId) |
35 | XPValue.Value = PlayerDS.XP |
36 | RankValue.Value = PlayerDS.Rank |
37 | HeistsValue.Value = PlayerDS.Heists |
38 | PrestigeValue.Value = PlayerDS.Prestige |
39 | print ( "loaded " .. Player.Name .. "'s leaderstats" ) |
42 | PlayerDataStore:SetAsync(Player.UserId, nil ) |
47 | local function SetupPlayer(Player) |
49 | local Leaderstats = Instance.new( "Folder" ) |
50 | Leaderstats.Name = "perm" |
51 | Leaderstats.Parent = Player |
54 | local function createNewValue(valueName) |
55 | local newValue = Instance.new( "IntValue" ) |
56 | newValue.Name = valueName |
57 | newValue.Parent = Leaderstats |
62 | local XPValue, RankValue, HeistsValue, PrestigeValue |
64 | XPValue = createNewValue( "XP" ) |
65 | RankValue = createNewValue( "Rank" ) |
66 | HeistsValue = createNewValue( "Heists" ) |
67 | PrestigeValue = createNewValue( "Prestige" ) |
70 | LoadPlayerData(Player, XPValue, RankValue, HeistsValue, PrestigeValue) |
77 | SavePlayerData(Player) |
83 | Players.PlayerAdded:Connect(SetupPlayer) |