-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSyncing.lua
More file actions
79 lines (65 loc) · 1.99 KB
/
DataSyncing.lua
File metadata and controls
79 lines (65 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("myDataStore")
local function loadData(player, key)
local success, result = pcall(function()
return playerDataStore:GetAsync(player.UserId .. "-" .. key)
end)
if success then
return result
else
print("Failed to load " .. key .. " data for " .. player.Name)
warn(result)
return nil
end
end
local function saveData(player, key, value)
local success, result = pcall(function()
playerDataStore:SetAsync(player.UserId .. "-" .. key, value)
end)
if not success then
print("Failed to save " .. key .. " data for " .. player.Name)
warn(result)
end
end
game.Players.PlayerAdded:Connect(function(player)
local Attributes = Instance.new("Folder")
Attributes.Name = "EXAMPLE" --// NAME OF THE FOLDER YOU WANT THE VALUES TO BE PARENTED TO
Attributes.Parent = player
local dataKeys = {} --// NAMES OF THE DATA VALUES YOU WANT TO SAVE AND LOAD GOES HERE
for _, key in ipairs(dataKeys) do
local value = Instance.new("NumberValue")
value.Name = key
value.Parent = Attributes
local loadedValue = loadData(player, key)
if loadedValue then
value.Value = loadedValue
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dataKeys = {} --// NAMES OF THE DATA VALUES YOU WANT TO SAVE AND LOAD GOES HERE
local dataUpdates = {}
for _, key in ipairs(dataKeys) do
local value = player:FindFirstChild("EXAMPLE"):FindFirstChild(key)
if value then
saveData(player, key, value.Value)
table.insert(dataUpdates, {
key = ("%d-%s"):format(player.UserId, key),
value = value.Value
})
end
end
local success, result = pcall(function()
playerDataStore:UpdateAsync(player.UserId, function(oldData)
local newData = oldData or {}
for _, update in ipairs(dataUpdates) do
newData[update.key] = update.value
end
return newData
end)
end)
if not success then
print("Failed to save data updates for " .. player.Name)
warn(result)
end
end)