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.
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.
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)