Parallel Lua Threads In Single Script?

I am currently programming a voxel game and trying to pipeline my code for chunk generation. I am unsure how I can write Parallel Lua in the same script if there is a way. What I understand about Parralel Lua is that it makes the whole script run in a different thread, Parallel to other scripts. So I’m wondering if there is some way to run parallel threads in the same scripts. Would using ConnectParallel run multiple threads for the same script in parallel?

local BindableEvent = script.Parent.Event
local BindableEvent2 = script.Parent.Event2
--thread one
BindableEvent:Fire()
--generate

BindableEvent.Event:ConnectParallel(function()
    --Compress
	BindableEvent2:Fire()
end)

BindableEvent2.Event:ConnectParallel(function()
    --render
end)

wait try using task.spawn(function() where the thread should be

so… would the code be someting like this?

local BindableEvent = script.Parent.Event
local BindableEvent2 = script.Parent.Event2

task.spawn(function()
    BindableEvent:Fire()
end)

--generate

BindableEvent.Event:ConnectParallel(function()
    --Compress
	BindableEvent2:Fire()
end)

BindableEvent2.Event:ConnectParallel(function()
    --render
end)

but im not sure where the thread is supposed to end or where the next one is supposed to begin

1 Like

Parallel lua requires scripts within many actors so there’s not really a way to do it in one script since each actor is 1 core, you could use the SendMessage function to easily parallelize everything across a lot of actors (no matter how many times you use connectparallel in one actor it will still use 1 core for that actor)
this explains it well: Parallel Luau | Documentation - Roblox Creator Hub

1 Like

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