Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local DSService = game:GetService("DataStoreService")
- local data = DSService:GetDataStore("Pets")
- Players.PlayerAdded:Connect(function(player)
- Instance.new("Folder",player).Name = "Pets"
- --Instance.new("Folder",player).Name = "PetsEquipped"
- Instance.new("NumberValue",player:WaitForChild("Main")).Name = "BoostCandy"
- Instance.new("NumberValue",player:WaitForChild("Main")).Name = "BoostCoins"
- local key = player.UserId
- local Pets = player:WaitForChild("Pets")
- for i,v in pairs(game.ReplicatedStorage:WaitForChild("Pets"):GetChildren()) do
- Instance.new("IntValue",Pets).Name = tostring(v)
- end
- wait(1)
- local tries = 0
- local max = 5
- local success
- local SavedLevel
- while not success and tries < max do
- success = pcall(function()
- SavedLevel = data:GetAsync(key)
- end)
- wait(tries)
- tries = tries + 1
- end
- if SavedLevel then
- for i, data in pairs(SavedLevel) do
- local pet = Pets:FindFirstChild(data[1])
- if pet then
- pet.Value = data[2]
- else
- local instance = Instance.new(data[3])
- if instance then
- instance.Name = data[1]
- instance.Value = data[2]
- instance.Parent = Pets
- end
- end
- end
- else
- data:SetAsync(key, {})
- end
- local autoSaveInterval = 180
- coroutine.resume(coroutine.create(function()
- while wait(autoSaveInterval) do
- local savedLevel = data:GetAsync(key)
- local objData = {}
- for i, obj in pairs(Pets:GetChildren()) do
- table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
- end
- local tries = 0
- local max = 5
- local success
- while not success and tries < max do
- success = pcall(function()
- if savedLevel == nil then
- data:SetAsync(key, objData)
- else
- data:UpdateAsync(key, function(oldValue)
- oldValue = objData
- return objData
- end)
- end
- end)
- tries = tries + 1
- wait(tries)
- end
- end
- end))
- end)
- Players.PlayerRemoving:Connect(function(player)
- local key = player.UserId
- local savedLevel = data:GetAsync(key)
- local Pets = player:FindFirstChild("Pets")
- if not Pets then return end
- Pets = Pets:GetChildren()
- local objData = {}
- for i, obj in pairs(Pets) do
- table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
- end
- local tries = 0
- local max = 5
- local success
- while not success and tries < max do
- success = pcall(function()
- if savedLevel == nil then
- data:SetAsync(key, objData)
- else
- data:UpdateAsync(key, function(oldValue)
- oldValue = objData
- return objData
- end)
- end
- end)
- tries = tries + 1
- wait(tries)
- end
- end)
- game:BindToClose(function()
- for i, player in pairs(Players:GetPlayers()) do
- local key = player.UserId
- local savedLevel = data:GetAsync(key)
- local pets = player:FindFirstChild("Pets")
- if not pets then return end
- pets = pets:GetChildren()
- local objData = {}
- for i, obj in pairs(pets) do
- table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
- end
- local tries = 0
- local max = 5
- local success
- while not success and tries < max do
- success = pcall(function()
- if savedLevel == nil then
- data:SetAsync(key, objData)
- else
- data:UpdateAsync(key, function(oldValue)
- oldValue = objData
- return objData
- end)
- end
- end)
- tries = tries + 1
- wait(tries)
- end
- end
- wait(3)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement