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