How can I see how many players have item X in a game?

Hello, as you can see in the title, how can I connect to the DataStore and see how many players have item “X”?

2 Likes

I would recommend giving an example to make it more clear what you are trying to achieve.
In this instance, I would recommend using a table, and whenever a player joins to then add that player to the table.

-- Lets pretend that PlayerInventory already stores the player's inventory

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local InventoryData = DataStoreService:GetDataStore("PlayerInventory")

local playerDataTable = {}

local function addPlayerData(plr)
	local success, playerInventory = pcall(function()
		playerDataTable[#playerDataTable + 1] = InventoryData:GetAsync(plr.UserId)
	end)
end

Players.PlayerAdded:Connect(addPlayerData)

Now all you have to do is circulate that table using a for loop and check which player has a certain item in the inventory.

Granted, I would rather you use profile service as it makes situations like these much easier, and I have much more experience with profile service. With profile service, the table with profiles would already be created.

1 Like

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