local Player = game:GetService('Players').LocalPlayer
local Backpack = Player.Backpack
function GetToolsCount()
local Count = 0
for _,v in {unpack(Backpack:GetChildren()), unpack(Player.Character:GetChildren())} do
if v.ClassName == 'Tool' then
Count+=1
end
end
return Count
end
local Player = game:GetService("Players").LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local Character = Player.Character or Player.CharacterAdded:Wait()
local TextLabel = script.Parent
local function GetToolsCount()
local Count = 0
for _, v in ipairs(Backpack:GetChildren()) do
if v:IsA("Tool") then
Count += 1
end
end
for _, v in ipairs(Character:GetChildren()) do
if v:IsA("Tool") then
Count += 1
end
end
return Count
end
local function UpdateLabel()
TextLabel.Text = "Tools: " .. GetToolsCount()
end
Backpack.ChildAdded:Connect(UpdateLabel)
Backpack.ChildRemoved:Connect(UpdateLabel)
Character.ChildAdded:Connect(UpdateLabel)
Character.ChildRemoved:Connect(UpdateLabel)
Player.CharacterAdded:Connect(function(newChar)
Character = newChar
Character.ChildAdded:Connect(UpdateLabel)
Character.ChildRemoved:Connect(UpdateLabel)
UpdateLabel()
end)
UpdateLabel()
local Players = game:GetService("Players")
local TextLabel = script.Parent
local plr = Players.LocalPlayer::Player
local PlayerGui = plr:WaitForChild("PlayerGui")::PlayerGui
local function CharAdded(char:Model):()
local backpack = plr:WaitForChild("Backpack")::Backpack
local function UpdateCount():()
TextLabel.Text = tostring(#backpack:GetChildren()+((char:FindFirstChildOfClass("Tool") and 1) or 0))
end
backpack.ChildAdded:Connect(UpdateCount)
backpack.ChildRemoved:Connect(UpdateCount)
UpdateCount()
end
plr.CharacterAdded:Connect(CharAdded)
local char = plr.Character::Model?
if char then CharAdded(char) end
but I could not get it to work on a surfaceGui and I can’t figure out why
here’s the layout
try testing this script, place it in a Script, and change the RunContext (on script properties) to “Client” and place the script inside the TextLabel
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Backpack = LocalPlayer:WaitForChild("Backpack") :: Backpack
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local TextLabel = script.Parent
local MaxTools = 38
local function UpdateToolsCount(Tool: Tool)
if Tool ~= nil then
if not Tool:IsA('Tool') then return end
end
task.defer(function()
local BackpackTools = #Backpack:GetChildren()
local CharacterTool = Character:FindFirstChildWhichIsA('Tool') and 1 or 0
TextLabel.Text = `{BackpackTools + CharacterTool}/{MaxTools}`
end)
end
Backpack.ChildAdded:Connect(UpdateToolsCount)
Backpack.ChildRemoved:Connect(UpdateToolsCount)
Character.ChildAdded:Connect(UpdateToolsCount)
UpdateToolsCount(nil)
local Player = game:GetService("Players").LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local Character = Player.Character or Player.CharacterAdded:Wait()
local TextLabel = script.Parent
local function GetToolsCount()
local Count = 0
for _, v in ipairs(Backpack:GetChildren()) do
if v:IsA("Tool") then
Count += 1
end
end
for _, v in ipairs(Character:GetChildren()) do
if v:IsA("Tool") then
Count += 1
end
end
return Count
end
local function UpdateLabel()
TextLabel.Text = "Tools: " .. GetToolsCount()
end
Backpack.ChildAdded:Connect(UpdateLabel)
Backpack.ChildRemoved:Connect(UpdateLabel)
Character.ChildAdded:Connect(UpdateLabel)
Character.ChildRemoved:Connect(UpdateLabel)
Player.CharacterAdded:Connect(function(newChar)
Character = newChar
Character.ChildAdded:Connect(UpdateLabel)
Character.ChildRemoved:Connect(UpdateLabel)
UpdateLabel()
end)
UpdateLabel()
and it works on textlabels in screenguis but not in any surfaceguis i make
Also if ur using ChildAdded and ChildRemoved, the might aswell not use GetToolsCount and just have a counter like u have right there and increment / decrement on those events.