· 4 years ago · Mar 16, 2021, 10:48 PM
1local DSS = game:GetService("DataStoreService")
2local Config = {
3 ["KeyName"] = "DataStore999";
4 ["Currencies"] = {
5 ["Cash"] = "Int"; -- "Int", "String", "Bool" (Int = number, String = text, Bool = true/false)
6 };
7}
8local Store = DSS:GetDataStore(Config.KeyName)
9local CurrenciesToSave = {}
10-- Create default currencies and put them in a table to be cloned
11for i, v in pairs(Config.Currencies) do
12 local new = Instance.new(v.."Value")
13 new.Name = i
14 table.insert(CurrenciesToSave, new)
15end
16
17-- PlayerAdded function
18local function Added(plr)
19 local PData = Instance.new("IntValue", plr)
20 PData.Name = "Data" -- Object's children are saved
21 for _, v in pairs(CurrenciesToSave) do
22 v:Clone().Parent = PData -- inserts Values into data
23 end
24 local Key = "Player_"..plr.UserId -- defines key
25 local Store = Store:GetAsync(Key) -- gets data
26 if Store then -- if data exists then overwrite values with data
27 for i, v2 in pairs(Store) do
28 PData[i].Value = v2
29 end
30 print("Loaded data for "..plr.Name)
31 else -- if not, then save the data
32 local TBL = {}
33 for _, v3 in pairs(PData:GetChildren()) do
34 TBL[v3.Name] = v3.Value
35 end
36 local s, f = pcall(function() -- pcall
37 Store:UpdateAsync(Key, function()
38 return TBL
39 end)
40 end)
41 if s then
42 print("Data saved succesfully.")
43 else -- retry if failed
44 print("Data saving failed. Retrying.")
45 local s2, f2 = pcall(function()
46 Store:UpdateAsync(Key, function()
47 return TBL
48 end)
49 end)
50 if s2 then
51 print("Retry succesfull.")
52 else
53 print("Retry failed.")
54 end
55 end
56 end
57end
58
59local function Removing(plr) -- on player remove
60 local PData = plr.Data -- Player's data
61 local TBL = {} - table of data to save
62 for _, v in pairs(PData:GetChildren()) do
63 TBL[v.Name] = v.Value -- makes values with the Value's name and Value
64 end
65 local Key = "Player_"..plr.UserId -- key again
66 local s, f = pcall(function() -- pcall again
67 Store:UpdateAsync(Key, function()
68 return TBL
69 end)
70 end)
71 if s then
72 print("Data Saving Succesfull!")
73 else -- retry again
74 print("Data saving failed. Retrying.")
75 local s2, f2 = pcall(function()
76 Store:UpdateAsync(Key, function()
77 return TBL
78 end)
79 end)
80 if s2 then
81 print("Retry succesful")
82 else
83 print("Retry failed.")
84 end
85 end
86end
87
88local function bindtoclose()
89 for _, plr in pairs(game:GetService("Players"):GetPlayers()) do -- save all player's data
90 local PData = plr.Data
91 local TBL = {}
92 for _, v in pairs(PData:GetChildren()) do
93 TBL[v.Name] = v.Value
94 end
95 local Key = "Player_"..plr.UserId
96 local s, f = pcall(function()
97 Store:UpdateAsync(Key, function()
98 return TBL
99 end)
100 end)
101 if s then
102 print("Data Saving Succesfull!")
103 else
104 print("Data saving failed. Retrying.")
105 local s2, f2 = pcall(function()
106 Store:UpdateAsync(Key, function()
107 return TBL
108 end)
109 end)
110 if s2 then
111 print("Retry succesful")
112 else
113 print("Retry failed.")
114 end
115 end
116 end
117end
118-- connections
119game:GetService("Players").PlayerAdded:Connect(Added)
120game:GetService("Players").PlayerRemoving:Connect(Removing)