Why is it saying there is 0 tools in Backpack?

Hi, so I have this line of code:

local backpack = player:FindFirstChildOfClass("Backpack")
print("Inventory size: " .. #backpack:GetChildren())

But it says: “Inventory size: 0” when I run the code when I clearly have a tool in my hotbar/backpack. Anyone know how to fix this and how I can get the tools in the current hotbar? Thank you.

1 Like

Server-script:

local ToolsInBackpack = 0

for _, V in pairs(Player.Backpack:GetChildren()) do 
	if V:IsA("Tool") then 
		ToolsInBackpack += 1 
	end
end

print(ToolsInBackpack) -- Prints out however many tools the player has in their backpack

This script checks if the player has any tools in their backpack and each time this script finds a tool in their backpack, the ToolsInBackpack variable number increases.

Thanks, but can I use the player:

local player = game.Players:GetPlayerFromCharacter(Char)

From a script to do this as well? Thank you.

UPDATE: I’m sorry but it says there is 1 tool when the tool is unequipped and 0 when it is equipped. How can I get rid of the uneqipped tool? Thanks.

EDIT: Nevermind, I found out how. Thanks everyone.

I will make an improvement to the code I have provided. What this does is it checks if there are any tools in the players character as well as their backpack:

local ToolsInBackpack = 0

for _, V in pairs(Player.Character:GetChildren()) do 
	if V:IsA("Tool") then 
		ToolsInBackpack += 1 
	end
end

for _, V in pairs(Player.Backpack:GetChildren()) do 
	if V:IsA("Tool") then 
		ToolsInBackpack += 1 
	end
end

print(ToolsInBackpack)

Thanks for all the help, I think I have it under control now. :slight_smile: I used some of your code to achieve this so have a solution.

1 Like

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