How would I make a script that would count every hat in a player's inventory?

Basically I want to make a script that counts every hat in a player’s inventory, I know I have to use the Inventory API, but I don’t even know where to start. Help is appreciated.


how do i fix that?

First create a LocalScript at StarterGui. Then add an IntValue and don’t change the name of it.

-- Waiting for player character to load
wait(1)

-- Getting correct user
local Username = script.Parent.Parent.Name
local User = game.Workspace:FindFirstChild(Username)

-- Getting player's accessories
for i,v in pairs(User:GetChildren()) do
   if v:IsA("Accessory") then
      script.Value.Value = script.Value.Value + 1
   end
end

-- Prints player's hat value
wait(1)
print(Username .. " has: " .. script.Value.Value .. " hats")
1 Like

thank you so much!! :slight_smile:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local appearance = player.CharacterAppearanceLoaded:Wait()
local hats = 0

for _, hat in ipairs(character:GetChildren()) do
	if hat:IsA("Accessory") then
		hats += 1
	end
end

print(player.Name.." is wearing "..hats.." accessories!")

This is a lot more optimised.

1 Like