Hello, im making a Inventory System and it works perfectly, But i have two question in this post
- Is this a good approach?
- How do i make item have stack like in minecraft, if player have item with a quantity of 215, then it split the item in 4 with max stack of 64, what are technique i can use for this since i dont know any math (ik this is more of scripting support but my code already works and im more concerned about first question)
function InventoryRefresh()
for _,obj in pairs(InvList:GetChildren()) do
if not obj:IsA("UIGridLayout") then
obj:Destroy()
end
end
end
function UpdateInventory()
local playerdata = GetPlayerData:InvokeServer() -- this just get playerdata like inventory, stat (i dont place them in player folder but make player object and store their stuff there)
if not playerdata then print("error") return end
InventoryRefresh()
for item_id,quantity in pairs(playerdata["plrinventory"]) do
local newitem = itemslot:Clone()
newitem.itemimg.Image = IItem[tonumber(item_id)].item_img
newitem.count.Text = quantity
newitem.Parent = InvList
newitem.Name = IItem[tonumber(item_id)].item_name
newitem.MouseButton1Click:Connect(function()
--use item if its usable
end)
end
end
as you can see, i heard memory leak happens when theres too much connection, but my script make it so if an item is added to player inventory, it DESTROY everything in inventory and then readd them, making an extra connection for all the item_buttons that was destroyed, i researched that connection automatically disconnect when the instance is deleted but i also heard from other post that function get disconnected on instance destroyed but not events so im concerned and im looking to what i can improve for this code so it doesnt cause problems in the future?
also my secondary question is what technique do i do to split the item in stack? like i want it to happen in client inventory instead of making my player inventory in server be like
[item_id:1] = 64,
[item_id:1] = 64,
[item_id:1] = 64,
cuz that solution i saw when i search and it be bad, i want the player inventory to be [item_id:1] = 192 then the client inventory local script does the work of splitting it, im having hard time to explain but i hope u get the point, what are approach i can do for this? thanks