I’ve recently noticed that a lot of players are losing their Pet Data in my game, so I created a new save mode that doesn’t force the server so much and doesn’t pass lag information, but for some reason DataStore2 doesn’t seem to save anything when I activate this script to change the players’ data table if anyone has any idea what the problem might be, let me know, this error has been a headache.
local DataStore2 = require(5159892169)
DataStore2.Combine("MyGameData_Version_1","PlayerPetData_V2","NewPetDataStore")
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
function getLevel(totalXP)
local Increment = 0
local RequiredXP = 100
for i = 0, RS.Pets.Settings.MaxPetLevel.Value do
RequiredXP = 100 + (25*i)
if totalXP >= (100*i) + Increment then
if i ~= RS.Pets.Settings.MaxPetLevel.Value then
if totalXP < ((100*i) + Increment) + RequiredXP then
return i
end
else
return i
end
end
Increment = Increment+(i*25)
end
end
function GrabData(Player) -- Grabs data for the player.
local Data = DataStore2("PlayerPetData_V2",Player)
Data:SetBackup(5)
local GrabData = Data:GetTable({["PetData"] = {}, ["PlayerData"] = {}})
if Data:IsBackup() then
Player:Kick("Sorry, but your data did not load correctly. Try rejoining to fix this problem...")
end
return GrabData
end
function NewGrabData(Player) -- Grabs data for the player.
local Data = DataStore2("NewPetDataStore",Player)
Data:SetBackup(5)
local GrabData = Data:GetTable({["DataPets"] = {}, ["InNewDate"] = false})
if Data:IsBackup() then
Player:Kick("Sorry, but your data did not load correctly. Try rejoining to fix this problem...")
end
return GrabData
end
Players.PlayerAdded:Connect(function(plr)
local Pets = Instance.new("Folder")
local Data = Instance.new("Folder")
Pets.Name = "Pets"
Data.Name = "Data"
local NewGrab = NewGrabData(plr)
if NewGrab.InNewDate == false then
local SavedData = GrabData(plr)
local NewTableSave = {}
for i,v in pairs(SavedData.PetData) do
table.insert(NewTableSave,v.PetID,{
Name = v.Name,
Equipped = v.Equipped,
TotalXP = v.TotalXP,
Type = v.Type,
})
end
local NewData = DataStore2("NewPetDataStore",plr)
NewData:Set({["DataPets"] = NewTableSave, ["InNewDate"] = true})
NewGrab = NewData:GetTable({["DataPets"] = {}, ["InNewDate"] = false})
print("Moving",plr,"for new datastore.")
else
print(plr,"is already in the new datastore!")
end
for PetID,PetInfo in pairs(NewGrab.DataPets) do
if RS.Pets.Models:FindFirstChild(PetInfo.Name) then
local PetObject = RS.Pets.PetFolderTemplate:Clone()
local Settings = RS.Pets.Models:FindFirstChild(PetInfo.Name).Settings
local TypeNumber = RS.Pets.CraftingTiers:FindFirstChild(PetInfo.Type).Value
local Level = getLevel(PetInfo.TotalXP)
PetObject.Name = PetInfo.Name
PetObject.Equipped.Value = PetInfo.Equipped
PetObject.TotalXP.Value = PetInfo.TotalXP
PetObject.Multiplier1.Value = Settings.Multiplier1.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
PetObject.Multiplier2.Value = Settings.Multiplier2.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
PetObject.PetID.Value = PetID
PetObject.Type.Value = PetInfo.Type
PetObject.Parent = Pets
end
end
for i,v in pairs(script.Data:GetChildren()) do
local DataValue = v:Clone()
DataValue.Parent = Data
if DataValue.Name == "MaxStorage" or v.Name == "MaxEquip" then
DataValue.Value = RS.Pets.Settings:FindFirstChild("Default".. DataValue.Name).Value
end
end
Pets.Parent = plr
Data.Parent = plr
local TableToSave = NewGrab
script.Parent.SavePetFolder.Event:Connect(function(Player,PetFolder,args)
if plr ~= Player then return end
if PetFolder.Parent then
TableToSave.DataPets[PetFolder.PetID.Value] = nil
table.insert(TableToSave.DataPets,PetFolder.PetID.Value,{
Name = PetFolder.Name,
Equipped = PetFolder.Equipped.Value,
TotalXP = PetFolder.TotalXP.Value,
Type = PetFolder.Type.Value,
})
print(Player,"Salved",PetFolder)
else
TableToSave.DataPets[args.PetID] = nil
print(Player,"Removed",PetFolder)
end
end)
spawn(function()
while true do
wait(1)
local PetData = TableToSave
local UpdateData = DataStore2("NewPetDataStore",plr)
UpdateData:Set(PetData)
end
end)
wait(5)
game.ReplicatedStorage.RemoteEvents.VerifyPlayerPetsNow:FireClient(plr)
end)