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 = "TestKey1" -- dla łatwego resetu bazy
- local remotes = game.ReplicatedStorage.Remotes
- local bank = remotes.Bank
- local area = remotes.Area
- 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)
- for key, folder in pairs(data) do
- if key == "leaderstats" then
- for item, amount in pairs(folder) do
- player.leaderstats[item].Value = amount
- end
- end
- if key == "Areas" then
- for item, _ 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[item].Barrier)
- end
- 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
- data[folder.Name] = {}
- for i, item in pairs(folder:GetChildren()) do
- data[folder.Name][item.Name] = item.Value
- 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.PlayerRemoving:Connect(SaveData)
- players.PlayerAdded:Connect(LoadData)
- 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