#How to count all tools with a specific name in inventory and count them even if the player has equip

41 messages · Page 1 of 1 (latest)

orchid cypress
#

How to count all tools with a specific name in inventory and count them even if the player has equiped that tool and i need to like count it always like if the tool was removed and if it was added beacause i need to keep a count of how much that tool i have in my inventory beacause im making a limit for that item? Or maybie is there a other way of making a limit of how much items you can have?

#

i even asked chatGPT

winged spruce
#

for loop

#

and getchildren

#

in backpack and your character

orchid cypress
#

yeah i understand that but somehow it doesnt work for me when i search in the character

vestal roverBOT
#

studio** You are now Level 1! **studio

winged spruce
orchid cypress
#

How to like paste code normaly

#

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Wait for hands to be available
local rightHand = character:WaitForChild("RightHand")
local leftHand = character:WaitForChild("LeftHand")

    -- Continuously check if the player is holding a tool
    game:GetService("RunService").Heartbeat:Connect(function()
        -- Check if the player is holding a tool in either hand
        if rightHand:FindFirstChildOfClass("Tool") or leftHand:FindFirstChildOfClass("Tool") then
            print(player.Name .. " is holding a tool!")
        else
            print(player.Name .. " is not holding any tool.")
        end
    end)
end)

end)

#

beacause i can oly paste like this

#

and i think need to rewrite the code

#

full code:

#

local function LogCounter(player)
local backpack = player.Backpack
local curentLogs = 0

for _, item in pairs(backpack:GetChildren()) do
    if item:IsA("Tool") and item.Name == "Log" then
        curentLogs = curentLogs + 1
    end
end
return curentLogs

end

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Wait for hands to be available
local rightHand = character:WaitForChild("RightHand")
local leftHand = character:WaitForChild("LeftHand")

    -- Continuously check if the player is holding a tool
    game:GetService("RunService").Heartbeat:Connect(function()
        -- Check if the player is holding a tool in either hand
        if rightHand:FindFirstChildOfClass("Tool") or leftHand:FindFirstChildOfClass("Tool") then
            print(player.Name .. " is holding a tool!")
        else
            print(player.Name .. " is not holding any tool.")
        end
    end)
end)

end)

robust moss
#

There are so many oddities in this script.

  1. why check every frame? why do you even need to constantly check?
  2. Why are you looking under the hands? equipped tools are always direct children of the character (if you're using the basic build in tools that is.)
function CountTools(player:Player, toolName:string):number
  local count = 0
  for _, tool in player.Backpack do
    if tool.Name ~= toolName then continue end
    count += 1
  end
if Player.Character and Player.Character[ToolName] then count += 1end
  return count
end

--probably not the most efficient, but we're talking about a #array of less than 100 entrees, no real need to micro-optimise at this scale
orchid cypress
#

so first i am checking very much beacause i need to always have a value bc i need to like drop that item maybie pick it up so i need the value to be exact and it need to change fast so i chek very much

#

second this was chatGPT mistake(im a starter so i use chatGPT)

robust moss
#

No need to have them be tools to be frank

orchid cypress
robust moss
#

Then have them stack in 1 slot

#

same effect, no need to have every one be it's own tool

orchid cypress
robust moss
orchid cypress
#

like i tried that

robust moss
#

But let's be frank, you do still appear extremely new to luau, so I would just recommend stopping this project and starting off with something way easier, e.g. some basic clicker simulator/tycoon/obby

orchid cypress
#

but i think it will be a bit glitchy

robust moss
orchid cypress
robust moss
#

Or well, as long as you don't rely on roblox' built in dropping system. You can easily mimic it by just doing :Clone() translate position forward a bit, parent = workspace. Then if the item count == 0 then tool:Destroy() 🤷‍♂️

orchid cypress
#

ima go and redo that -4h of my life wasted

robust moss
robust moss
orchid cypress
#

like i have a srver side script and a local side script for item droping

robust moss
#

You're currently mostly working with chatgpt, if you did something easier than you could've done most of it yourself

orchid cypress
vestal roverBOT
#

studio** You are now Level 2! **studio

orchid cypress
#

Case closed

somber pasture
# orchid cypress How to count all tools with a specific name in inventory and count them even if ...

do something like...


local function check(name)
local amount = 0
local name = name
for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
if v.Name == name then
amount = amount + 1
end
end)

if game.Players.LocalPlayer.Character:FindFirstChild(name) then
amount = amount + 1
end

return amount
end

--now call the function
local gunAmt = check("Gun")
print("Player has .. " .. gunAmt " amount of guns.")


orchid cypress
#

Omg thx