So recently i’m working on inventory system, the hardest one in my entire game, and the thing is that i’m scared of memory leaks ect. game will be based around it and one module is crucial for it to work
local Item = {}
-- Settings
local MaxPickupDistance = 24
local DropOffset = Vector3.new(0,-2,-5)
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Important stuff
local remote = ReplicatedStorage.Remotes.PickAndDrop
local Inventories = ReplicatedStorage.Inventories
local ItemData = require(ReplicatedStorage.ObjectsData.ItemsData)
-- Class to folder
local ClassInfo = {
["Tool"] = {
["Folder"] = "SingleItems",
["ValueType"] = "BoolValue"
},
["Item"] = {
["Folder"] = "Inventory",
["ValueType"] = "IntValue"
},
["Food"] = {
["Folder"] = "Inventory",
["ValueType"] = "IntValue"
}
}
-- Drop item in front of player
function Item:Drop(item: ValueBase, CharacterCFrame: Vector3)
local name = item.Name
if item:IsA("IntValue") then
item.Value -= 1
if item.Value <= 0 then item:Destroy() end
else
item:Destroy()
end
local newModel = game:GetService("ServerStorage").ItemModels[name]:Clone()
newModel.Name = name
newModel.CFrame = CharacterCFrame * CFrame.new(DropOffset)
newModel.Parent = workspace.Items
end
-- Pick item if it's in range of player
function Item:Pick(item: BasePart, player: Player)
if (item.Position - player.Character.PrimaryPart.Position).Magnitude > MaxPickupDistance then return end
local name = item.Name
local Class = ItemData[name].Class
local FolderName = ClassInfo[Class].Folder
local ValueType = ClassInfo[Class].ValueType
local Folder = Inventories[player.UserId][FolderName]
if not Folder:FindFirstChild(name) or FolderName == "SingleItems" then
local NewValue = Instance.new(ValueType)
NewValue.Name = name
if ValueType == "IntValue" then NewValue.Value = 1 else NewValue.Value = false end
NewValue.Parent = Folder
else
Folder[name].Value += 1
end
item:Destroy()
end
return Item
What it does is drops or picks item that was checked by Main script, yk debounce, if it’s real or if it’s actual item, not other random part.
At the end, will this code lag if used by a lot of players, take in mind that there is 0.1 second debounce each time player can drop or pick an item