Last active
September 14, 2021 23:54
-
-
Save lucasmz-dev/451ffa6174d2b8dff38a539570521e6d to your computer and use it in GitHub Desktop.
ParallelSpawn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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