Skip to content

Instantly share code, notes, and snippets.

@lucasmz-dev
Last active September 14, 2021 23:54
Show Gist options
  • Save lucasmz-dev/451ffa6174d2b8dff38a539570521e6d to your computer and use it in GitHub Desktop.
Save lucasmz-dev/451ffa6174d2b8dff38a539570521e6d to your computer and use it in GitHub Desktop.
ParallelSpawn
local Bindable: BindableEvent = Instance.new("BindableEvent")
type Event = typeof(Bindable.Event)
local Event: Event = Bindable.Event
return function(_function: (...any) -> (), ...)
if typeof(_function) ~= "function" then
error("Must be a function", 2)
end
local connection: RBXScriptConnection = nil
local argCount: number = select("#", ...)
if argCount == 0 then
connection = Event:ConnectParallel(_function)
else
local args: {[number]: any} = {...}
connection = Event:ConnectParallel(function()
_function(
table.unpack(args, 1, argCount)
)
end)
end
Bindable:Fire()
connection:Disconnect()
end
-- task.spawn/desyncronize solution:
-- This is way faster than using bindables.
-- This doesn't error, which can be useful to you if you just wanna *prepare* your game for it,
-- (AFAIK need to confirm) task.desyncronize doesn't error in live-games or studio,
-- It simply just makes it into parallel if it can be, or stays in the main thread if it can't.
-- Not erroring at all.
-- It is your choice which one you use, and for what.
local function RunFunctionInParallel(_function: (...any) -> (), ...)
task.desynchronize()
_function(...)
end
return function(_function: (...any) -> (), ...)
if typeof(_function) ~= "function" then
error("Must be a function", 2)
end
task.spawn(RunFunctionInParallel, _function, ...)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment