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")
- -- tworzenie/odczytywanie bazy danych o nazwie "Data"
- local dataStore = dataStoreService:GetDataStore("Data")
- -- do szybkiego resetu bazy
- local key = "TestKey6"
- local remotes = game.ReplicatedStorage.Remotes
- local bank = remotes.Bank
- local area = remotes.Area
- local createTemplate = remotes.CreateTemplate
- local function LoadData(player)
- for i, folder in pairs(playerData:GetChildren()) do
- folder: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
- 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
- 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
- if key == "Codes" then
- for code, value in pairs(folder) do
- local usedCode = Instance.new("StringValue", player.Codes)
- usedCode.Name = code
- 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 folder.Name == "Pets" then
- data[folder.Name][item.Value] = item.Name
- else
- data[folder.Name][item.Name] = item.Value
- 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
- player.leaderstats[currency].Value += amount
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement