· 6 years ago · Dec 08, 2019, 05:56 PM
1local playerDataManager = {}
2
3local dataStoreService = game:GetService("DataStoreService")
4local playerItemData = dataStoreService:GetDataStore("PlayerItems") --the datastore that holds player items. Note that a datastore holds info for ALL players, not just one
5
6currentPlayerData = {} --a table that contains the players data
7
8function playerDataManager:playerAddItem(player, itemId, itemAmount)
9 local playerId = "Player_" .. player.UserId
10 if currentPlayerData[playerId][itemId] then --if in the player data them itemId already exists then add the itemamount(value) to the itemId(key)
11 currentPlayerData[playerId][itemId] = currentPlayerData[playerId][itemId] + itemAmount
12 else
13 currentPlayerData[playerId][itemId] = itemAmount --if theres no player data than add the itemId key and set itemamount value
14 end
15end
16
17function playerDataManager:loadPlayerData(player)
18 local playerId = "Player_" .. player.UserId
19 local data = playerItemData:GetAsync(playerId) --gets player data from the PlayerItems datastore if it exists
20 if data then --loads the player data if they have played before
21 currentPlayerData[playerId] = data
22 for item, amount in pairs(currentPlayerData[playerId]) do
23 print ("player " .. player.Name .. " has " .. item .. amount)
24 end
25 else
26 currentPlayerData[playerId] = {WoodWall = 0, StoneWall = 0}
27 end
28end
29
30function playerDataManager:returnItems(player)
31 local playerId = "Player_" .. player.UserId
32 local data = playerItemData:GetAsync(playerId) --gets player data from the PlayerItems datastore if it exists
33 if currentPlayerData[playerId] then --loads the player data if they have played before
34 return currentPlayerData[playerId] --assign the data to the currentplayerdata table
35 else
36 return "no data"
37 end
38end
39
40local function savePlayerData(playerId)
41 if currentPlayerData[playerId] then --if player has data in the player data table
42 playerItemData:SetAsync(playerId, currentPlayerData[playerId]) --save for playerid the current players data to Player Items datastore
43 end
44end
45
46function playerDataManager:savePlayerDataOnExit(player)
47 local playerId = "Player_" .. player.UserId
48 savePlayerData(playerId)
49end
50
51function playerDataManager:autoSave()
52 while wait(60) do
53 for playerId, data in pairs(currentPlayerData) do
54 savePlayerData(playerId)
55 print ("data saved")
56 end
57 end
58end
59
60--[[
61game.Players.PlayerAdded:Connect(function(player)
62 playerDataManager:loadPlayerData(player) --load player data on game open
63 playerDataManager:playerAddItem(player, "WoodWall", 1)
64end)
65
66spawn(playerDataManager:autoSave()) --create function autosave in its own thread seperate from the main game - allows an endless loop without locking other processes
67
68game.Players.PlayerRemoving:Connect(function(player)
69 playerDataManager:savePlayerDataOnExit(player) --save player data on exit
70end)
71--]]
72
73return playerDataManager