DataStore2 Problem with :Get()

Hi there!
I’m trying to add pets to my game and I need a system that checks if the player can equip a pet and if they can equips it. I hit a wall in my coding.

Problem:

Using DataStore2("Pets", plr):Get({}) in scripts returns and empty table, {}, even if I set it to {“Pet”}

This doesnt occur when I try to :Get() a data store from the console.
Heres my script (part of a module):

local module = {}
local random = math.random
local DS2 = require(game:GetService("ServerScriptService").Data.DataStore2)
local Notify = require(game:GetService("ServerScriptService").Notifications).Notify
local ExisitingPets = {"Weak Brick", "Rob", "Mechanic", "Bob", "Goat", "Bawb"}
-- Equip Pets --
function canEquipPetAgain(plr: Player, Pet, Pets, EquippedPets)
	print(Pets:Get()) -- This returns {}
	print(EquippedPets:Get())  -- This returns {}
	local MaxEquipablePets = 3
	-- --
	local FoundPetsInventory = 0
	for _, LoopPet in pairs(Pets:Get({})) do
		if LoopPet == Pet then
			FoundPetsInventory += 1
		end
	end
	local FoundPetsEquipped = 0
	for _, LoopPet in pairs(EquippedPets:Get({})) do
		if LoopPet == Pet then
			FoundPetsEquipped += 1
		end
	end
	-- Correcting --
	if FoundPetsEquipped > FoundPetsInventory or #EquippedPets:Get({}) > MaxEquipablePets then
		Notify(plr, "The game noticed an error and corrected it.\nYour pets have been unequipped due to an error.", Color3.new(1, 0, 0))
	end
	-- Returning --
	if FoundPetsEquipped+1 > FoundPetsInventory then
		print(FoundPetsEquipped+1 .. " ".. FoundPetsInventory)
		Notify(plr, "You already equipped this pet.", Color3.new(1, 0, 0))
		return false
	elseif #EquippedPets:Get({})+1 > MaxEquipablePets then
		Notify(plr, "You already equipped " .. #EquippedPets:Get({}) / MaxEquipablePets .. " Pets.", Color3.new(1, 0, 0))
		return false
	elseif FoundPetsEquipped+1 <= FoundPetsInventory then
		return true
	else
		Notify(plr, "Something went wrong. (NRCEPAF)", Color3.new(1, 0, 0))
		return false
	end
end

function module.EquipPet(plr:Player, Pet:string)
	local Pets = DS2("Pets", plr)
	local EquippedPets = DS2("EquippedPets", plr)
	-- --
	--[[if Pets:Get({}) == nil or #Pets:Get({}) == 0 or not table.find(ExisitingPets, Pet) or not table.find(Pets:Get({}), Pet) then
		Notify(plr, "Something went wrong (NPEP)", Color3.new(1, 0, 0))
		return
	end]]
	-- --
	if canEquipPetAgain(plr, Pet, Pets, EquippedPets) == false then
		return
	end
	-- --
	if EquippedPets:Get({}) == nil or #EquippedPets:Get({}) == 0 and ExisitingPets[Pet] then
		EquippedPets:Set({Pet})
		return
	elseif EquippedPets:Get({}) ~= nil and #EquippedPets:Get({}) ~= 0 and ExisitingPets[Pet] then
		local oldTable = EquippedPets:Get({})
		oldTable[#oldTable+1] = Pet
		local newTable = oldTable
		EquippedPets:Set(newTable)
		return
	end
end

Thanks for you help in advance :slight_smile:

It could be due to trying to get the data too fast when a player joins.

1 Like

nah, the player must use a button in a menu at the moment so that cant be it since they have to be loaded in fully to use it

FIgured something out on my own, something went wrong with my code when using DS2.Combine, thanks for your help :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.