· 5 years ago · May 24, 2020, 06:28 PM
1--[ Variables ]--
2local playerService = game:GetService("Players");
3local coinsData = game:GetService("DataStoreService"):GetDataStore("CoinsData");
4
5--[[
6-- The function, saves the data for our player
7-- on the PlayerRemoved event.
8--
9-- @param {data-type} player | We pars the argument player as the parameter of our function.
10]]--
11function saveDataForThePlayer(player)
12 --[ Player unique Key ]--
13 local userId = ("PlayerKey_"..player.UserId);
14
15 local data = {}; -- it'll be holding our values
16
17 --[[
18 -- On this function we use `pcall()` a error handling function
19 -- that provides the Vanilla Lua (the Lua version that uses ROBLOX).
20 ]]--
21 local succes, err = pcall(function()
22 for _, stat in pairs(player:WaitForChild("leaderstats"):GetChildren()) do
23 data[stat.Name] = stat.Value;
24 end
25
26 -- we send a queue to our DataStore API so it can save the data for the player
27 coinsData:SetAsync(userId, data);
28 end)
29
30 --[[
31 -- Error handling, if the succes is provided false then
32 -- we return the error and now we can obviously, fix it
33 ]]--
34 if not succes and err then return err end
35end
36
37--[[
38-- This event listener, trigeers whenever a player joins the server
39-- we pars our parameter player as the player that has joined our server.
40]]--
41playerService.PlayerAdded:connect(function(player)
42
43 --[ Player unique Key ]--
44 local userId = ("PlayerKey_"..player.UserId);
45
46 --[ Folder creation ]--
47 local folder = Instance.new("Folder");
48 folder.Name = "leaderstats"; -- this will show on the leaderboard
49 folder.Parent = player;
50
51 --[ Creating our values ]--
52 local currency = Instance.new("IntValue");
53 currency.Name = "Coins";
54 currency.Parent = folder;
55
56 local data = coinsData:GetAsync(userId);
57
58 --[[
59 -- Verifies if our player has data saved
60 -- if it's provided true, then it loads the data
61 -- else, creates a new data for our player.
62 ]]--
63 if not data and data == nil then
64 print(player.Name.." doesn't has data saved, we are gonna create new one.");
65 elseif data and data ~= nil then
66 print(player.Name.." we are loading your data, dont worry!");
67
68 --[[
69 -- We start changing the values of our currencies
70 -- to our current data saved.
71 ]]--
72 for _, stat in pairs(folder:GetChildren()) do
73 stat.Value = data[stat.Name]
74 end
75 end
76end)
77
78--[[
79-- Event listener PlayerRemoved to actually save our player data
80-- after he leaves we queue the DataStore to save it.
81]]--
82playerService.PlayerRemoving:connect(function(player)
83 local err = saveDataForThePlayer(player);
84 if err then print(err) return end
85end)
86
87--[[
88-- Function :BindToClose() will save the Data of the player as a emergency if the game shutdown
89-- or just falls or something that cause the instan close of the game.
90]]--
91game:BindToClose(function()
92
93 --[[
94 -- We start saving the data for all the player
95 -- that are connected to our Server.
96 --]]
97 for _, player in pairs(playerService:GetPlayers()) do
98 local err = saveDataForThePlayer(player)
99 if err then print(err) return end
100 end
101end)