Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local players = game:GetService("Players")
- local playerData = script.PlayerData
- local dataStoreService = game:GetService("DataStoreService")
- local dataStore = dataStoreService:GetDataStore("Data")
- local key = "TestKey2"
- local remotes = game.ReplicatedStorage.Remotes
- local bank = remotes.Bank
- local area = remotes.Area
- local createTemplate = remotes.CreateTemplate
- local function LoadData(player)
- for i, v in pairs(playerData:GetChildren()) do
- v:Clone().Parent = player
- end
- local data
- local success, errorMsg = pcall(function()
- data = dataStore:GetAsync(player.UserId .. key)
- if not data then
- return
- end
- for key, folder in pairs(data) do
- if key == "leaderstats" then
- for item, amount in pairs(folder) do
- if item == "Coins" then
- player.leaderstats.Coins.Value = amount
- elseif item == "Diamonds" then
- player.leaderstats.Diamonds.Value = amount
- end
- end
- end
- if key == "Areas" then
- for item, amount in pairs(folder) do
- if not player.Areas:FindFirstChild(item) then
- local value = Instance.new("StringValue", player.Areas)
- value.Name = item
- area:FireClient(player, workspace.Areas[value.Name].Barrier)
- end
- end
- end
- if key == "Pets" then
- for id, petName in pairs(folder) do
- local pet = Instance.new("StringValue", player.Pets)
- pet.Name = petName
- pet.Value = id
- createTemplate:FireClient(player, petName, id)
- end
- end
- end
- print("Loaded data")
- print(data)
- end)
- if not success then
- warn(errorMsg)
- wait(5)
- LoadData(player)
- end
- end
- local function SaveData(player)
- local data = {}
- local count = 0
- local success, errorMsg = pcall(function()
- for i, folder in pairs(player:GetChildren()) do
- if folder:IsA("Folder") then
- data[folder.Name] = {}
- for i, item in pairs(folder:GetChildren()) do
- if item.Value then
- if folder.Name == "Pets" then
- data[folder.Name][item.Value] = item.Name
- else
- data[folder.Name][item.Name] = item.Value
- end
- end
- end
- end
- end
- dataStore:SetAsync(player.UserId .. key, data)
- end)
- if not success then
- warn(errorMsg)
- wait(5)
- SaveData(player)
- end
- print("Saved data")
- print(data)
- end
- players.PlayerAdded:Connect(LoadData)
- players.PlayerRemoving:Connect(SaveData)
- bank.OnServerEvent:Connect(function(player, action, currency, amount)
- if action == "+" then
- if currency == "Coins" then
- player.leaderstats.Coins.Value += amount
- elseif currency == "Diamonds" then
- player.leaderstats.Diamonds.Value += amount
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement