Help with Counting Items Globally

Hello Developers,
I don’t really have the time right now to type something super fancy and proffesional but here’s what I’m trying to achieve:

So basically I want to build a system which will be able to tell everyone in a server how many items of a type are globally in the game, including both in offline & online players’ inventory.

I first thought of making a simple DataStore and Incrementing a key each time a new item of that type is getting created, but after thinking a little about the rate limiting DataStores have, I don’t really think it’s a good idea.

Anyone has any idea of how I can make this kind of system that will also work on a large-scale number of players?

Well if you store those items already in a data store, just count how many they have, or you could do your approach, just would have to change something up. You could make it so every minute (60s) the datastore gets updated.

here is a simple script, i used datastoreService, its better to use DataStore2 though, this is a very simple one, you can alter it so it is way better

local messagingService = game:GetService("MessagingService")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("itemsOwned")

messagingService:SubscribeAsync("addItem" , function(message)
	local item = message.itemName
	local jobId = message.jobId
	if jobId ~= game.JobId then --checking its from another server so it doesnt get added twice
		local success , value
		
		repeat
			success , value = dataStore:GetAsync(item.."aRandomKey")
		until success
		
		value += 1
		
		repeat
			success , value = dataStore:SetAsync(item.."aRandomKey" , value)
		until success
	end
end)

--when an item gets added

messagingService:PublishAsync("addItem" , {item = "something" , jobId = game.JobId})
1 Like

you should probably run a for i v in pairs loop of the players inside the Player instance, then you do like v.Backpack (Whatever is the path to the items), you could run this every X amount of seconds if you put it in a while loop, then, when its ended, you could create an IntValue so the client can read when the intvalue changes (Intvalue.Changed) and then change a GUI or whatever you want it to show on. Thats if you want it on the client.