· 5 years ago · Oct 30, 2020, 09:28 PM
1--[[
2
3 Leaderstats save script. This script works with a variable number of leaderstats values and with custom names.
4
5 To add a new value, simply add another Value object inside the createLeaderstats() function and give it a unique name.
6
7 They can be any of these types: IntValue, StringValue, NumberValue, BoolValue. any other types of values will NOT save in the DataStore.
8
9--]]
10
11local PlayerService = game:GetService("Players")
12local DataStoreService = game:GetService("DataStoreService")
13local DataStore = DataStoreService:GetDataStore("Leaderstats")
14
15local function createLeaderstats(player)
16 local leaderstats = Instance.new("Folder", player)
17 leaderstats.name = "leaderstats"
18
19 local kos = Instance.new("IntValue", leaderstats)
20 kos.Name = "KOs"
21
22 local deaths = Instance.new("IntValue", leaderstats)
23 deaths.Name = "Deaths"
24
25
26 --[[
27 -- example leaderstats values. you can comment and uncomment this block and the script
28 -- will automatically save all the values inside the 'leaderstats' folder
29 -- NOTE: the stats must have unique names. you probably already knew that but just in case
30
31 local coins = Instance.new("IntValue", leaderstats)
32 coins.Name = "Coins"
33
34 local spree = Instance.new("IntValue", leaderstats)
35 spree.Name = "Spree"
36 --]]
37
38end
39
40local function loadData(player)
41 -- data variable to store the loaded data
42 local data
43 local success, errorMessage = pcall(function()
44 data = DataStore:GetAsync(player.UserId)
45 end)
46
47 -- if the pcall ran successfully and the data exists, load it in
48 if success and data then
49
50 -- look for the player leaderstats
51 local leaderstats = player:FindFirstChild("leaderstats")
52 if leaderstats then
53 -- leaderstats folder was found
54 -- loop through each stat in the folder and load the value from the 'data' table
55 for index, stat in pairs(leaderstats:GetChildren()) do
56 stat.Value = Data[stat.Name]
57 end
58 end
59 else
60 warn("Could not load data for", player.Name .. ":", errorMessage)
61 end
62end
63
64local function saveData(player)
65 -- initializing data table to be used for saving
66 local data = {}
67
68 -- look for the player leaderstats
69 local leaderstats = player:FindFirstChild("leaderstats")
70 if leaderstats then
71 -- leaderstats folder was found
72 -- loop through each stat in the folder and add the value to the 'data' table
73 for index, stat in pairs(leaderstats:GetChildren()) do
74 data[stat.Name] = stat.Value
75 end
76 end
77
78 local success, errorMessage = pcall(function()
79 DataStore:SetAsync(player.UserId, data)
80 end)
81
82 if success then
83 print("Data was saved for", player.Name)
84 else
85 warn("Could not save data for", player.Name .. ":", errorMessage)
86 end
87end
88
89
90function onPlayerAdded(player)
91
92 -- create the leaderstats
93 createLeaderstats(player)
94
95 -- load in the player's data
96 loadData(player)
97
98end
99
100function onPlayerRemoving(player)
101
102 -- save data
103 saveData(player)
104
105end)
106
107game:BindToClose(function()
108 -- this function makes sure that player data is saved even if the server is shut down
109 repeat wait() until #PlayerService:GetPlayers() < 1
110end)
111
112PlayerService.PlayerAdded:Connect(onPlayerAdded)
113PlayerService.PlayerRemoving:Connect(onPlayerRemoving)
114