Hey Devs,
I’m trying to make a system where if you pick up a tool, with a proximity prompt, and the tool inventory limit is passed, I want to replace the currently equipped tool instead of deleting it as I have currently in the script. If anyone knows how to help, please do! Here is the script i currently have which is a server script in server script service
local inventoryLimit = 3
function getTools(plr:Player)
local tools = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
table.insert(tools, tool)
end
for _, child in pairs(plr.Character:GetChildren()) do
if child:IsA("Tool") then
table.insert(tools, child)
end
end
return tools
end
function checkIfOverLimit(plr:Player)
local plrTools = getTools(plr)
if #plrTools > inventoryLimit then
for i = 1, (#plrTools - inventoryLimit) do
local toolToRemove = plrTools[i]
game:GetService("RunService").Heartbeat:Wait()
toolToRemove.Parent = workspace
toolToRemove.Handle.CFrame = plr.Character.HumanoidRootPart.CFrame - plr.Character.HumanoidRootPart.CFrame.LookVector * 5
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr:WaitForChild("Backpack").ChildAdded:Connect(function()
checkIfOverLimit(plr)
end)
plr.CharacterAdded:Connect(function(char)
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
checkIfOverLimit(plr)
end
end)
end)
end)
I thought this is the script that make the player replace the tool and it’s not working right, is that what you are looking for?:
local inventoryLimit = 1
function getTools(parent)
local tools = {}
for _,child in pairs(parent:GetChildren()) do
if child:IsA("Tool") then
table.insert(tools, child)
end
end
return tools
end
function checkIfOverLimit(plr:Player)
local charTools = getTools(plr.Character)
local bpTools = getTools(plr.Backpack)
local plrToolsCount = #charTools + #bpTools
if plrToolsCount > inventoryLimit then
for i,tool in ipairs(bpTools) do
if i > inventoryLimit then
local toolToRemove = bpTools[i]
game:GetService("RunService").Heartbeat:Wait()
toolToRemove.Parent = workspace
toolToRemove.Handle.CFrame = plr.Character.HumanoidRootPart.CFrame - plr.Character.HumanoidRootPart.CFrame.LookVector * 5
end
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
checkIfOverLimit(plr)
end
end)
end)
end)
btw, in the proximity script you should make the Triggered event equip the tool directly instead of adding it to the backpack:
local pp = script.Parent
pp.Triggered:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
hum.EquipTool(pp.Parent)
end)