So I’m trying to make a system that basically counts the number of objects and sets the count for the inventory, but it’s not reliable because sometimes the loading is too quick for the events to register?
Basically the code below will end up with this result, in which it will arrange the objects accordingly and simply add to the x3 (counter) when more of the same objects are included in the player object folder which I use to keep track of inventory.
I would like to ask for any alternative methods or approaches I can have to address my goal. I do not need a fix for my code.
Player:FindFirstChild("Objects").ChildAdded:Connect(function(v)
if Player.Objects:FindFirstChild(tostring(v)) then
if Player.PlayerGui.EditPanel.Inventory.Holder:FindFirstChild(tostring(v)) then
script.Parent.Inventory.Holder:FindFirstChild(tostring(v)).Amount.Value += 1
script.Parent.Inventory.Holder:FindFirstChild(tostring(v)).Count.Text = "x"..script.Parent.Inventory.Holder:FindFirstChild(tostring(v)).Amount.Value
else
local x = game.ReplicatedStorage.GuiTemplates:FindFirstChild(tostring(v.Name)):Clone()
x.Parent = script.Parent.Inventory.Holder
end
end
end)
Player:FindFirstChild("Objects").ChildRemoved:Connect(function(v)
if Player.Objects:FindFirstChild(tostring(v)).Amount.Value == 1 then
Player.PlayerGui.EditPanel.Inventory.Holder:FindFirstChild(tostring(v)):Destroy()
elseif Player.Objects:FindFirstChild(tostring(v)).Amount.Value > 1 then
script.Parent.Inventory.Holder:FindFirstChild(tostring(v)).Amount.Value -= 1
script.Parent.Inventory.Holder:FindFirstChild(tostring(v)).Count.Text = "x"..script.Parent.Inventory.Holder:FindFirstChild(tostring(v)).Amount.Value
end
end)