Help me with DataStore2

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)
1 Like

I don’t use DS2, but I’m pretty sure you need to require the actual module and not the id.

I’ve been using DS2 for a while, it works with ModuleScript id, but for some reason it’s not saving in this specific script

Nope require the id will work too.

1 Like

I know that, but using the id is sometimes outdated.

You were supposed to add “DATA” first in DataStore2.Combine()

DataStore2.Combine("DATA","MyGameData_Version_1","PlayerPetData_V2","NewPetDataStore")

The MasterKey is “MyGameData_Version_1”

Oh, I didn’t notice.
Just in case, try using the actual module.

Edit: Also, use coroutine.wrap. It’s way better than spawn(function()

Btw for those lazy like me (that are used to using spawn) you can put this at the top of your scripts

local function spawn(f)
coroutine.wrap(f)()
end

or if you are a nonce and need help you can do

getfenv().spawn = function(i_am_a_nonce)
coroutine.wrap(i_am_a_nonce)()
end

Using coroutine.wrap() is simple. Especially in this form:

local coroutinefunction = coroutine.wrap(function()
while true do
wait(0.1)
print("coroutine")
end
end)

coroutinefunction()

It’s just for the lazies like me

1 Like

Also another thing I noticed is that you repeatedly set the pet’s data every second. You could just send a remote event to the server which that will save it. Save it every time the player gets a new pet.