More efficient and reliable alternative needed

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)

Can’t you just make a number value and set the name to the item and the total owned items of that name as the value? For like inventory. This will make it easier?

Instead of just adding new instance of that item.

Agreed. I honestly don’t know why I did the earlier part because it now seems extremely redundant. Converting to IntValue and just making small adjustments to my datastore made it much easier for the task to be accomplished. Thank you.

In the future, you should utilize :JSONEncode() & JSONDecode() (deprived from HttpService) when dealing with datastores to convert instances/tables into strings that datastores can interpret & compile.

Unless, you already know of this.

I haven’t found any practical use for encoding yet probably because the contents of my datastore aren’t significant enough, but I’ll probably give it a try when the situation calls for it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.