How do I limit how many tools a player can pick up?

not me posting twice within the hour nope

so, i’m making a custom inventory system. i’ve looked into suggestions, and most say to preform a check when the player picks up a new tool and if it’s more than what they’re supposed to carry. if it is, delete the tool. something like this:

--THIS IS JUST EXAMPLE STUFF IT'S NOT WHAT I'M ACTUALLY USING
local maxtools = 8
local player = game:GetService("Players").LocalPlayer
local chara = player.Character or player.CharacterAdded:Wait()
chara.ChildAdded:Connect(function(item)
	local invsize = player.Backpack:GetChildren()
	if item:IsA("Tool") and maxtools == #invsize then
		item:Destroy()
	end
end)

i want to try and avoid having to delete the tool-- i want to make it so if the player tries to pick up the tool by walking over it, it instead doesn’t go into the inventory at all and rather stays on the floor. is there a way to accomplish this?

Partial solution:
You could insert a script in each Tool (inside some part of it) and verify it from there, and not make it so general elsewhere, this way the tool is simply not added

local maxtools = 8
local Tool = script.Parent.Parent

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local invsize = Player.Backpack:GetChildren()
		
		if not maxtools == #invsize then
			Tool.Parent = Player.BackPack
		end
	end
end)
1 Like

this can work. if i can’t figure out a simpler solution, i’ll probably have the server insert this script into every tool in workspace

yeah u can set the tools parent to workspace and set it like a few studs away from the character das wat i do

player.Backpack.ChildAdded:Connect(function(child)
	wait(0.05)
	local ToolAmount = #player.Backpack:GetChildren()
	local CharacterTool = Character:FindFirstChildWhichIsA("Tool")
	
	if CharacterTool then
		ToolAmount = ToolAmount + 1
	end
	
	if ToolAmount >= 15 then
		child.Parent = workspace
		child.Handle.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,3,-6)
	end
end)
2 Likes
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local backpack = player:WaitForChild("Backpack")

backpack.ChildAdded:Connect(function(child)
	local toolCount = #backpack:GetChildren()
	if toolCount > 8 then
		child:Destroy() --destroy tool if 9th tool in backpack
	end
end)

Local script, place it inside the StarterPlayerScripts folder or the StarterCharacterScripts folder.

i specified in the post i didn’t want the tool to be destroyed, but rather left alone so other players can have the opportunity to pick it up. from what it looks like, this script does the opposite of what i want

I hadn’t read that far, one moment.

that’s a good idea, though i also wonder if this can be easily exploitable with players intentionally pushing the item to behind a wall or out of the map

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local backpack = player:WaitForChild("Backpack")

local function randNum()
	return math.random(-15, 15)
end

backpack.ChildAdded:Connect(function(child)
	local toolCount = #backpack:GetChildren()
	if toolCount > 8 then
		child.Parent = workspace
		local handle = child:WaitForChild("Handle")
		local hmr = character:WaitForChild("HumanoidRootPart")
		handle.CFrame = hmr.CFrame + CFrame.new(randNum(), 0, randNum()) 
	end
end)

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3

You can use this function to make sure handles of tools aren’t in places where they shouldn’t be.

1 Like

oh!! i’ve never heard of this function before!! i think this would help out a lot!
plus, what you also suggested, i think it could work. thank you!