ProfileService | How do i do pets count

I need a way to display the quantity of a specific pet stored in the game. For instance, if there are 30 Bats saved across many players using ProfileService, I want to find a way to print “30” to represent the total count of Bats saved on the ProfileService, or even just a way to print all of the data saved on ProfileService at once and then do the calculation from there.

The way im currently saving pets data in my game is by saving a table of the players pets into a profile data called Inventory:
image

I want to add that the data is ALREADY saved in the game across players, so I cant track of people that buy pets now since its too late, aswell as theres a trading system.

If anyone knows how to achieve this or has any insights on how to approach it, I would greatly appreciate your help.

Thanks in advance!

1 Like

You cannot really access other players’ data whilst they’re in another server due to session locking.

You could try using Datastore, example:

local datastore = game:GetService("Datstore")
local pets = datastore:GetGlobalDataStore("pet-id-0")
local data
repeat task.wait()
	suc = pcall(function()
		data= pets:GetSortedAsync(false,100)
	end)
until suc
local currentnumber = 0
for i,v in pairs(data) do
	if v.Value > currentnumber then currenumber = v.Value end
end
print(currentnumber)

and to set it you could do:

local datastore = game:GetService("Datstore")
local data
local pets = datastore:GetGlobalDataStore("pet-id-0")
repeat task.wait()
	suc = pcall(function()
		data= pets:GetSortedAsync(false,100)
	end)
until suc
local currentnumber = 0
for i,v in pairs(data) do
	if v.Value > currentnumber then currenumber = v.Value end
end
game:GetService("Datstore"):GetOrderedDataStore("pet-id-0"):SetAsync(currentnumber + 1,currentnumber + 1)

Or you could just use a external datastore im pretty sure.

Edit: also try to minimize the data getting

2 Likes

Or if you want u can use a external datastore (see message from another post): Need help with making a system that keeps track of how many an item exists - #27 by clxr

You could do it when the players joins it sends a request to update datastore if the id isnt found in the pet id datastores.