Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I make multiple functions run simultaneously from the same script?

Asked by
Trew86 175
6 years ago

I tried making this function to a coroutine but...

01module.Functions.Dispatch = coroutine.wrap(function(train, path, button)
02    if path == nil then return end
03    button.Text = path.Name
04    while wait() do
05        if path.Status == 0 then
06            button.TextColor3 = Color3.new(0, 170, 0)
07            button.Active = true
08        elseif path.Status == 1 then
09            button.TextColor3 = Color3.new(255, 255, 0)
10            button.Active = true
11        elseif path.Status == 2 then
12            button.TextColor3 = Color3.new(255, 0, 0)
13            button.Active = false
14        end
15        if train == nil then
View all 21 lines...

when I call it multiple times, only the first one starts.

1module.Functions.Dispatch(mod.QueuedTrain, mod.Paths[1], O1)
2module.Functions.Dispatch(mod.QueuedTrain, mod.Paths[2], O2)
3module.Functions.Dispatch(mod.QueuedTrain, mod.Paths[3], O3)

How can I run them at the same time? Thanks.

0
Only one coroutine ever runs at a time theking48989987 2147 — 6y

1 answer

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
6 years ago
Edited 6 years ago

Use spawn. I would avoid coroutine, unless you require some specific coroutine functionality.

1spawn(module.Functions.Dispatch(mod.QueuedTrain, mod.Paths[1], O1))
2spawn(module.Functions.Dispatch(mod.QueuedTrain, mod.Paths[2], O2))
3spawn(module.Functions.Dispatch(mod.QueuedTrain, mod.Paths[3], O3))

Edit: If you want spawn inside function, you can do it like this:

01module.Functions.Dispatch = function(train, path, button)
02    if path == nil then return end
03    button.Text = path.Name
04    spawn(function()
05        while wait() do
06            if path.Status == 0 then
07                button.TextColor3 = Color3.new(0, 170, 0)
08                button.Active = true
09            elseif path.Status == 1 then
10                button.TextColor3 = Color3.new(255, 255, 0)
11                button.Active = true
12            elseif path.Status == 2 then
13                button.TextColor3 = Color3.new(255, 0, 0)
14                button.Active = false
15            end
View all 23 lines...
Ad

Answer this question