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?
#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)
yeah i understand that but somehow it doesnt work for me when i search in the character
** You are now Level 1! **
what does your code look like
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)
There are so many oddities in this script.
- why check every frame? why do you even need to constantly check?
- 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
SORRY FOR REPLYING A BIT LATE
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)
You're better off only checking on events. Yet to be frank, this is a dumb way of handling inventories. It would be way easier to just have a basic UI with a number that goes up as you pick shit up, and goes down as you drop them
No need to have them be tools to be frank
i need to have tools in inventory beacause like i want so you can equip them
Then have them stack in 1 slot
same effect, no need to have every one be it's own tool
HOW???? i didnt know you can stack items in roblox studio
By faking it, item picked up -> number += 1, item dropped? item -= 1
like i tried that
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
but i think it will be a bit glitchy
It shouldn't be
k and it will not be if the number is in the player as a int?
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() 🤷♂️
ima go and redo that -4h of my life wasted
I've not used intvalues in ages, but it shouldn't matter
Which is why you should start off with something easier, so you can get a feeling for it
like i have a srver side script and a local side script for item droping
You're currently mostly working with chatgpt, if you did something easier than you could've done most of it yourself
I only like try to program and test my skill when even chatGPT doesnt help i hope i dont use it in the future
** You are now Level 2! **
Case closed
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.")
Omg thx