How make it so in this 3 items inventory limit script, a tool gets destroyed the second the tool count exceeds 3? Since, there’s a bug where it only happens if I equip a new tool after having a full inventory already, instead of a random old tool getting deleted and the new tool taking the slot the deleted old tool was in instantly, on top of that, this happens aswell (notice how instead of the new tool on itemslot 4 doesn’t take the place of the old tool that was deleted from itemslot 1, and instead is just in itemslot 4):
Here is my script:
local inventoryLimit = 3
function getTools(plr)
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)
local toolsInBackpack = plr.Backpack:GetChildren()
local toolsInCharacter = {}
for _, child in pairs(plr.Character:GetChildren()) do
if child:IsA("Tool") then
table.insert(toolsInCharacter, child)
end
end
while #toolsInBackpack + #toolsInCharacter > inventoryLimit do
local toolToRemove
if #toolsInBackpack > 0 then
toolToRemove = table.remove(toolsInBackpack, 1)
else
toolToRemove = table.remove(toolsInCharacter, 1)
end
toolToRemove:Destroy()
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr:WaitForChild("Backpack").ChildAdded:Connect(function(newTool)
-- Check if the new tool addition exceeds the limit
checkIfOverLimit(plr)
end)
plr.CharacterAdded:Connect(function(char)
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
-- Check if equipping the new tool exceeds the limit
checkIfOverLimit(plr)
end
end)
end)
end)
We can use an IntValue and do .Changed to check for changes in the value. If this value exceeds 3 as you wish, then we can implement the code you want.
local IntValue = YOUR_INT_VALUE_LOCATION_HERE;
IntValue.Changed:Connect(function(value)
if value > 3 then
-- code here
end
end)
We can use Attributes to store the number of weapons you have, then use :GetAttributeChangedSignal() to check for changes.
local Character = plr.Character or plr.CharacterAdded:Wait() -- whatever your character is
Character:GetAttributeChangedSignal("NumberOfWeapons"):Connect(function(changed)
if changed > 3 then
-- code here
end
end)
Use these to your liking and see if they work.
I hope this helps!
So, for the first method, if the value changes & exceeds 3, it then executes the code? Did I understand that right? And the attributes method will only work with certain tools/will have to use a list of sorts to then execute the code?
Yep, that’s correct. If the value changes at any point, the event will fire. The parameter ‘value’ is the new value that’s given to you. You’re checking if that value is greater than 3, and if it is then it executes the code.
local value = IntValue_Location -- value is 0
value.Changed:Connect(function(newValue)
print(newValue) -- whenever the value is added, this will print with the new value
end)
The attributes method works by setting an attribute on your character. Attributes are like values, except they are better in terms of performance and should be used on players/characters. The attributes method essentially works the same as the first method. It’s really just your own personal preference.