For example a player goes to a proxminity prompt and activates it, i want the script to show how many tools the player has and what tools they are.
What do i do? Do i do player.tools:GetChildren()?
local Tools = 0
for i,v in pairs(Player.Character:GetChildren()) do
if v:IsA("Tool") then
Tools += 1
end
end
for i,v in pairs(Player.Backpack:GetChildren()) do
if v:IsA("Tool") then
Tools += 1
end
end
print(Tools)
it checks the character too because when a player has their tool equipped its parented to the players character
very simple ngl
local child = Player.Backpack:GetChildren()
for i = 1,#child do
print(child[i].Name)
print(#child)
end
I know nobody uses HopperBins anymore, but good to add more compatibility anyways
local Tools = 0
for i,v in pairs(Player.Character:GetChildren()) do
if v:IsA("Tool") or v:IsA("HopperBin") then
Tools += 1
end
end
for i,v in pairs(Player.Backpack:GetChildren()) do
if v:IsA("Tool") or v:IsA("HopperBin") then
Tools += 1
end
end
print(Tools)
Just slightly modified @soggyfeet13’s code.
No idea if you need to check for a HopperBin inside of their character, I haven’t used them in ages. But it doesn’t hurt not to.
Same as @TestAccount563344 and @soggyfeet13 but a bit more improved (at least for me):
local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local Tools = 0
RunService.RenderStepped:Connect(function()
for i,v in pairs(Player.Character:GetChildren()) do
if v:IsA("Tool") or v:IsA("HopperBin") then
Tools = i
end
end
for i,v in pairs(Player.Backpack:GetChildren()) do
if v:IsA("Tool") or v:IsA("HopperBin") then
Tools = i
end
end
print(Tools)
end)
it is checking Tool number every render step. If you want constant checking then you can use this but if you want just once then use @TestAccount563344 ’ s and @soggyfeet13 's. I wrote because maybe it could be useful.
local tools = 0
for i, v in pairs(character:GetChildren()) do
if v:IsA("Tool") then tools += 1 end
end
tools += #player.Backpack:GetChildren()
the shortest way
Also note that “#” means “number” so that’s just the number of all the backpack’s children.
function numberoftools(backpack)
local tools = 0 -- if this is outside the function, the number will only keep going up unless you actually set it back to 0 yourself.
for i,v in pairs(backpack:GetChildren()) do
if v:IsA('Tool') then -- make sure it's a tool and not a script or something.
tools += 1
end
end
if player.Character:FindFirstChildOfClass('Tool') then -- if the player has a tool equipped, it'll be in their character, not their backpack.
tools += 1
end
return tools
end
print(numberoftools(player.Backpack))
Just use numberoftools(player.Backpack) and the game will basically just read the whole function as just the number of tools.
Sorry about weird format, I’m on mobile lol