Hello Roblox,
i wanted to make a inventory limit code, and this is my code:
local limit = 4
local player = game.Players.LocalPlayer
local tools = player.Backpack:GetChildren()
local toolnumber = 0
local character = player.Character
local hasToolEquipped
for _,v in pairs(player.Backpack:GetChildren()) do
toolnumber = toolnumber + 1
end
while true do
wait(0.1)
if character:FindFirstChildOfClass("Tool") then
hasToolEquipped = true
else
hasToolEquipped = false
end
print(player.Backpack:GetChildren())
print(toolnumber)
end
but i dont know what to do now that it works.
I looked in the forum and didnt get a answer…
please help me!
In this script, whenever a tool is added to the player’s backpack the script checks if the player has too many tools or not. If they have too many tools then the tool will go back to the workspace (RETURN_TOOL_PARENT), and if the player has room for the tool then nothing will happen.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local toolCount = 0
local LIMIT = 4
local RETURN_TOOL_PARENT = workspace -- Where the tool will go if the player cannot pick it up
player.Backpack.ChildAdded:Connect(function(child)
if not child:IsA("Tool") then return end
toolCount += 1 -- New tool being added
if toolCount > LIMIT then
child.Parent = RETURN_TOOL_PARENT
end
end)
player.Backpack.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then toolCount -= 1 end
end)
local players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local backpack = player:WaitForChild("Backpack")
local toolCount = #backpack:GetChildren()
backpack.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
toolCount += 1
end
end)
backpack.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then
toolCount -= 1
end
end)
character.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
toolCount += 1
end
end)
character.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then
toolCount -= 1
end
end)
Here’s a version which tracks tools added to the player’s character/removed from the player’s character as well. This way if a tool is lost (not added to either backpack or character) then the tool count is correctly decremented by 1.