SHOW:
|
|
- or go back to the newest paste.
1 | local players = game:GetService("Players") | |
2 | ||
3 | local playerData = script.PlayerData | |
4 | ||
5 | local dataStoreService = game:GetService("DataStoreService") | |
6 | local dataStore = dataStoreService:GetDataStore("Data") | |
7 | local key = "TestKey2" | |
8 | ||
9 | local remotes = game.ReplicatedStorage.Remotes | |
10 | local bank = remotes.Bank | |
11 | local area = remotes.Area | |
12 | local createTemplate = remotes.CreateTemplate | |
13 | ||
14 | local function LoadData(player) | |
15 | for i, v in pairs(playerData:GetChildren()) do | |
16 | v:Clone().Parent = player | |
17 | end | |
18 | ||
19 | local data | |
20 | ||
21 | ||
22 | local success, errorMsg = pcall(function() | |
23 | ||
24 | data = dataStore:GetAsync(player.UserId .. key) | |
25 | ||
26 | if not data then | |
27 | return | |
28 | end | |
29 | ||
30 | for key, folder in pairs(data) do | |
31 | if key == "leaderstats" then | |
32 | for item, amount in pairs(folder) do | |
33 | if item == "Coins" then | |
34 | player.leaderstats.Coins.Value = amount | |
35 | elseif item == "Diamonds" then | |
36 | player.leaderstats.Diamonds.Value = amount | |
37 | end | |
38 | end | |
39 | end | |
40 | ||
41 | if key == "Areas" then | |
42 | for item, amount in pairs(folder) do | |
43 | if not player.Areas:FindFirstChild(item) then | |
44 | local value = Instance.new("StringValue", player.Areas) | |
45 | value.Name = item | |
46 | area:FireClient(player, workspace.Areas[value.Name].Barrier) | |
47 | end | |
48 | end | |
49 | end | |
50 | ||
51 | if key == "Pets" then | |
52 | for id, petName in pairs(folder) do | |
53 | local pet = Instance.new("StringValue", player.Pets) | |
54 | pet.Name = petName | |
55 | pet.Value = id | |
56 | createTemplate:FireClient(player, petName, id) | |
57 | end | |
58 | end | |
59 | end | |
60 | ||
61 | print("Loaded data") | |
62 | print(data) | |
63 | end) | |
64 | ||
65 | if not success then | |
66 | warn(errorMsg) | |
67 | wait(5) | |
68 | LoadData(player) | |
69 | end | |
70 | end | |
71 | ||
72 | ||
73 | local function SaveData(player) | |
74 | local data = {} | |
75 | local count = 0 | |
76 | ||
77 | local success, errorMsg = pcall(function() | |
78 | ||
79 | for i, folder in pairs(player:GetChildren()) do | |
80 | if folder:IsA("Folder") then | |
81 | data[folder.Name] = {} | |
82 | ||
83 | for i, item in pairs(folder:GetChildren()) do | |
84 | if item.Value then | |
85 | if folder.Name == "Pets" then | |
86 | data[folder.Name][item.Value] = item.Name | |
87 | else | |
88 | data[folder.Name][item.Name] = item.Value | |
89 | end | |
90 | end | |
91 | end | |
92 | end | |
93 | end | |
94 | ||
95 | dataStore:SetAsync(player.UserId .. key, data) | |
96 | end) | |
97 | ||
98 | if not success then | |
99 | warn(errorMsg) | |
100 | wait(5) | |
101 | SaveData(player) | |
102 | end | |
103 | ||
104 | print("Saved data") | |
105 | print(data) | |
106 | end | |
107 | ||
108 | players.PlayerAdded:Connect(LoadData) | |
109 | players.PlayerRemoving:Connect(SaveData) | |
110 | ||
111 | bank.OnServerEvent:Connect(function(player, action, currency, amount) | |
112 | if action == "+" then | |
113 | if currency == "Coins" then | |
114 | player.leaderstats.Coins.Value += amount | |
115 | elseif currency == "Diamonds" then | |
116 | player.leaderstats.Diamonds.Value += amount | |
117 | end | |
118 | end | |
119 | end) |