How would I use parallel Luau in an Inventory System?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to know the most efficient way to do this inventory system.
  2. What is the issue? Include screenshots / videos if possible!
    I’m not sure how to implement parallel Luau.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I found nothing.

I know how to use task.desynchronize() and task.synchronize() with scripts under an actor, but I’m not sure how to make a script run with other actors for anything in general. In my case, I want to make a inventory system with Parallel Luau to load the items as fast as possible. I think I need actors with the same exact script running in Parallel, but I’m not sure.

In what way are you loading these items that you’d have a need to desynchronize? It’s generally not something that requires such an extreme measure.

If you explain the process in detail maybe we can pinpoint an issue there, if there is an issue at all.

1 Like

Well heres how it works. When the player loads in, the server scripts would detect that and then load the items they have off a datastore. But before that, It would run a reapeat loop until all the data loaded. Then, it would run in a for loop for the folder that contains the data, and it would run in Parallel for if statements for the data.

Here is my basic idea:

local ss = game:GetService("ServerStorage")
task.desynchronize()
local p = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent

repeat
	task.wait(.1)
until p:FindFirstChild("InventoryItems")
task.wait(1)

local invitems = p.InventoryItems

for i, v in pairs(invitems:GetChildren()) do
	if v.Value ~= 0 then
		if not script.Parent.Parent:FindFirstChild(v.Name) then
			local ssitem = ss.Storage.Items:FindFirstChild(v.Name)
			task.synchronize()
			local template = script.template:Clone()
			template.Name = v.Name
			template.ImageButton.Image = ss:GetAttribute("ImgId")
			template.ImageButton.UIStroke.Color = ssitem:GetAttribute("RarityColor")
			template:SetAttribute("Count",v.Value)
			template:SetAttribute("ItemName",v.Name)
			template:SetAttribute("ItemType",ssitem:GetAttribute("ItemType"))
			template:SetAttribute("Rarity",ssitem:GetAttribute("Rarity"))
			template:SetAttribute("Worth",ssitem:GetAttribute("Worth"))
			template:SetAttribute("RarityColor",ssitem:GetAttribute("RarityColor"))
			
			if v.Value >= 2 then
				template.howmany.Visible = true
				template.howmany.Text = v.Value.."x"
			end
		end
	end
end

No need for desync there, and no need to run a loop like that, that’s what WaitForChild is for.

local InventoryItems = game:GetService('Players').LocalPlayer:WaitForChild('InventoryItems')

local function onItemAdded(Item: IntValue)
    -- handle UI
end

InventoryItems.ChildAdded:Connect(onItemAdded)
for _, Item in InventoryItems:GetChildren() do onItemAdded(Item) end

I’m typing on my phone so hopefully that’s all correct. I assumed from your code that it’s a LocalScript. If it is, then this is fine, because if InventoryItems is never added it probably means that the player data failed to load and the player should be kicked because of that anyways (after a more than 1 attempt of course). If it’s a Script then you shouldn’t use WaitForChild like that, so you don’t have an infinitely yielding thread.

You definitely don’t need to use Actors for this.

Okay, thank you for this, will do.

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