Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local DS = game:GetService("DataStoreService"):GetDataStore("PetsSave")
- local SaveOnChange = true
- local Cooldown = 0
- local function Thread(f)
- coroutine.resume(coroutine.create(f))
- end
- local Array = {
- ["Stats"] = {
- },
- ["PlayerValues"] = {
- },
- ["Pets"] = {
- ['CurrentPet'] = '',
- ["Orange puppy"] = 'unowned',
- ["Amazing Pet"] = 'unowned',
- ["Uncommon Pet"] = 'unowned',
- ["Unobtainable Pet"] = 'unowned',
- ["Rare Pet"] = 'unowned',
- ["Premium Pet"] = 'unowned',
- ["Monster Pet"] = 'unowned',
- }
- }
- Players.PlayerAdded:connect(function(plr)
- for i,v in pairs(Array.PlayerValues) do
- local x
- if type(v) == "number" then
- x = Instance.new("NumberValue", plr)
- x.Name = tostring(i)
- x.Value = v
- elseif type(v)=="string" then
- x=Instance.new("StringValue",plr)
- x.Name=tostring(i)
- x.Value=v
- elseif type(v)=="boolean" then
- x=Instance.new("BoolValue",plr)
- x.Name=tostring(i) x.Value=v
- end
- end
- local ID = plr.userId
- if DS:GetAsync("User_"..tostring(ID)) == nil then
- print("Player "..plr.Name.."("..tostring(ID)..") has no data file, creating a new data file.")
- local NewData=DS:SetAsync("User_"..tostring(ID),Array)
- end
- local Data = DS:GetAsync("User_"..tostring(ID))
- repeat wait() until Data ~= nil
- print("User_"..tostring(ID).." Data Loading...")
- --Create leaderstats
- local Stats = Instance.new("IntValue",plr)
- Stats.Name = "leaderstats"
- --Create Pets
- local Pets = Instance.new("Configuration",plr)
- Pets.Name = "Pets"
- Thread(function()
- for i,v in pairs(Array.Stats) do
- local x
- if type(v) == "number" then
- x = Instance.new("NumberValue",Stats)
- x.Name = tostring(i)
- x.Value = Data.Stats[i] or v
- elseif type(v) == "string" then
- x = Instance.new("StringValue",Stats)
- x.Name = tostring(i)
- x.Value = Data.Stats[i] or v
- elseif type(v) == "boolean" then
- x = Instance.new("BoolValue",Stats)
- x.Name = tostring(i)
- x.Value = Data.Stats[i] or v
- end
- if x == nil or x["Value"] == nil or SaveOnChange == false then
- return
- end
- local Deb = false
- x.Changed:connect(function(val)
- Deb=true
- local NewArray = {["Stats"]={};["Pets"]={}}
- Thread(function()
- for i,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
- NewArray.Stats[v.Name] = v.Value
- end
- end)
- for i,v in pairs(plr:WaitForChild("Pets"):GetChildren()) do
- NewArray.Pets[v.Name]=v.Value
- end
- DS:UpdateAsync("User_"..tostring(ID), function(OldVal)
- local NewVal=NewArray or OldVal
- return NewVal
- end)
- wait(Cooldown)
- Deb=false
- end)
- end
- end)
- for i,v in pairs(Array.Pets) do
- local x
- if type(v) == "number" then
- x = Instance.new("NumberValue", Pets)
- x.Name = tostring(i)
- x.Value = Data.Pets[i] or v
- elseif type(v)=="string" then
- x=Instance.new("StringValue",Pets)
- x.Name=tostring(i)
- x.Value=Data.Pets[i] or v
- elseif type(v)=="boolean" then
- x=Instance.new("BoolValue",Pets)
- x.Name=tostring(i) x.Value=Data.Pets[i] or v
- end
- if x==nil or x["Value"]==nil or SaveOnChange==false then
- return
- end
- local Deb=false
- x.Changed:connect(function(val)
- Deb=true
- local NewArray={["Stats"]={};["Pets"]={}}
- Thread(function()
- for i,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
- NewArray.Stats[v.Name] = v.Value
- end
- end)
- for i,v in pairs(plr:WaitForChild("Pets"):GetChildren()) do
- NewArray.Pets[v.Name]=v.Value
- end
- DS:UpdateAsync("User_"..tostring(ID),function(OldVal)
- local NewVal = NewArray or OldVal
- return NewVal
- end)
- wait(Cooldown)
- Deb=false
- end)
- end
- print(plr.Name.."("..tostring(ID)..")'s Data is ready.")
- end)
- Players.PlayerRemoving:connect(function(plr)
- local ID=plr.userId
- local Data=DS:GetAsync("User_"..tostring(ID))
- if Data==nil then
- return
- end
- local NewArray={["Stats"]={};["Pets"]={}}
- Thread(function()
- for i,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
- NewArray.Stats[v.Name]=v.Value
- end
- end)
- for i,v in pairs(plr:WaitForChild("Pets"):GetChildren()) do
- NewArray.Pets[v.Name]=v.Value
- end
- DS:UpdateAsync("User_"..tostring(ID), function(OldVal)
- local NewVal = NewArray or OldVal
- return NewVal
- end)
- end)
- local function SaveAll()
- for _,v in ipairs(Players:GetPlayers()) do
- Thread(function()
- local ID = v.userId
- local Data = DS:GetAsync("User_"..tostring(ID))
- if Data ~= nil then
- local NewArray = {["Stats"] = {}; ["Pets"] = {}}
- Thread(function()
- for _,v2 in pairs(v:WaitForChild("leaderstats"):GetChildren()) do
- NewArray.Stats[v2.Name] = v2.Value
- end
- end)
- for _,v2 in pairs(v:WaitForChild("Pets"):GetChildren()) do
- NewArray.Pets[v2.Name] = v2.Value
- end
- DS:UpdateAsync("User_"..tostring(ID), function(OldVal)
- local NewVal=NewArray or OldVal
- return NewVal
- end)
- end
- end)
- end
- end
- game:BindToClose(function()
- SaveAll()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement