· 4 years ago · Jan 10, 2021, 07:18 AM
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 _, stat in pairs(leaderstats:GetChildren()) do
56 stat.Value = data[stat.Name]
57 end
58 end
59
60 print("Loaded data for", player.Name..":", data)
61 else
62 warn("Could not load data for", player.Name .. ":", errorMessage)
63 end
64end
65
66local function saveData(player)
67 -- initializing data table to be used for saving
68 local data = {}
69
70 -- look for the player leaderstats
71 local leaderstats = player:FindFirstChild("leaderstats")
72 if leaderstats then
73 -- leaderstats folder was found
74 -- loop through each stat in the folder and add the value to the 'data' table
75 for _, stat in pairs(leaderstats:GetChildren()) do
76 data[stat.Name] = stat.Value
77 end
78 end
79
80 local success, errorMessage = pcall(function()
81 DataStore:SetAsync(player.UserId, data)
82 end)
83
84 if success then
85 print("Data was saved for", player.Name)
86 else
87 warn("Could not save data for", player.Name .. ":", errorMessage)
88 end
89end
90
91
92function onPlayerAdded(player)
93
94 -- create the leaderstats
95 createLeaderstats(player)
96
97 -- load in the player's data
98 loadData(player)
99
100end
101
102function onPlayerRemoving(player)
103
104 -- save data
105 saveData(player)
106
107end
108
109game:BindToClose(function()
110 -- this function makes sure that player data is saved even if the server is shut down
111 repeat wait(3) until #PlayerService:GetPlayers() < 1
112end)
113
114PlayerService.PlayerAdded:Connect(onPlayerAdded)
115PlayerService.PlayerRemoving:Connect(onPlayerRemoving)