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"
- 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
- 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, _ 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
- end
- print("Loaded data")
- print(data)
- end)
- if not success then
- warn(errorMsg)
- wait(5)
- LoadData(player)
- end
- end
- -- Funkcja 'SaveData', która zapisuje dane gracza na serwerze
- local function SaveData(player)
- -- Definiujemy zmienną 'data' i
- -- przeglądamy wszystkie dzieci gracza, zapisując ich wartości do zmiennej 'data'
- local data = {}
- 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
- -- Wywołujemy funkcję 'SetAsync' na obiekcie 'dataStore', zapisując dane gracza
- dataStore:SetAsync(player.UserId .. key, data)
- end)
- -- Jeżeli wykonanie zadania nie udało się, wyświetlamy błąd i ponownie zapisujemy dane
- if not success then
- warn(errorMsg)
- wait(5)
- SaveData(player)
- end
- print("Saved data")
- print(data)
- end
- -- Podpinamy funkcje 'LoadData' i 'SaveData' do eventów 'PlayerAdded' i 'PlayerRemoving'
- players.PlayerAdded:Connect(LoadData)
- players.PlayerRemoving:Connect(SaveData)
- -- Podpinamy funkcję do eventu 'OnServerEvent' na obiekcie 'bank',
- -- umożliwiającej graczom dodawanie środków do konta
- 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