local maxItemLimit = 3 -- number of tools allowed
local dropDistance = 10 -- how far in front of the player to drop the tool
-- just used to wait, since newly parented objects can throw an error if you try to change their parent too quick
local runService = game:GetService('RunService')
game:GetService('Players').PlayerAdded:Connect(function(player)
-- when a character dies, a new backpack object is created, so we can't wait for it inside the scope of the first function
player.CharacterAdded:Connect(function(character)
-- wait for humanoid and backpack to exist
local humanoid = character:WaitForChild('Humanoid')
local backpack = player:WaitForChild('Backpack')
-- equipped tools are parented to character, not backpack, so we need to account for both
local function getTotalTools()
local totalTools = 0
totalTools += #backpack:GetChildren()
for _, object in pairs(character:GetChildren()) do
if object:IsA('Tool') then
totalTools += 1
end
end
return totalTools
end
-- if new child is a tool and player is at tool capacity, drop new item 10 studs in front of them
local function checkToolCount(child)
if not child:IsA('Tool') then return end
if getTotalTools(backpack, character) > maxItemLimit then
runService.Heartbeat:Wait()
child.Parent = workspace
child:WaitForChild('Handle').Position = humanoid.RootPart.Position + (humanoid.RootPart.CFrame.LookVector * dropDistance)
end
end
-- check tool count whenever a child is parented to backpack or character
backpack.ChildAdded:Connect(function(child)
checkToolCount(child)
end)
-- when a tool is picked up, it's automatically equipped (parented to character instead of backpack)
character.ChildAdded:Connect(function(child)
checkToolCount(child)
end)
end)
end)
-- generate tool and clone to each players' backpack every second just for the sake of the example
local tool = Instance.new('Tool')
local handle = Instance.new('Part', tool)
handle.Size = Vector3.new(1, 1, 1)
handle.Name = 'Handle'
while wait(1) do
for _, player in pairs(game:GetService('Players'):GetPlayers()) do
tool:Clone().Parent = player:WaitForChild('Backpack')
end
end