I tried making this function to a coroutine but...
01 | module.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.TextColor 3 = Color 3. new( 0 , 170 , 0 ) |
07 | button.Active = true |
08 | elseif path.Status = = 1 then |
09 | button.TextColor 3 = Color 3. new( 255 , 255 , 0 ) |
10 | button.Active = true |
11 | elseif path.Status = = 2 then |
12 | button.TextColor 3 = Color 3. new( 255 , 0 , 0 ) |
13 | button.Active = false |
14 | end |
15 | if train = = nil then |
when I call it multiple times, only the first one starts.
1 | module.Functions.Dispatch(mod.QueuedTrain, mod.Paths [ 1 ] , O 1 ) |
2 | module.Functions.Dispatch(mod.QueuedTrain, mod.Paths [ 2 ] , O 2 ) |
3 | module.Functions.Dispatch(mod.QueuedTrain, mod.Paths [ 3 ] , O 3 ) |
How can I run them at the same time? Thanks.
Use spawn. I would avoid coroutine, unless you require some specific coroutine functionality.
1 | spawn(module.Functions.Dispatch(mod.QueuedTrain, mod.Paths [ 1 ] , O 1 )) |
2 | spawn(module.Functions.Dispatch(mod.QueuedTrain, mod.Paths [ 2 ] , O 2 )) |
3 | spawn(module.Functions.Dispatch(mod.QueuedTrain, mod.Paths [ 3 ] , O 3 )) |
Edit: If you want spawn inside function, you can do it like this:
01 | module.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.TextColor 3 = Color 3. new( 0 , 170 , 0 ) |
08 | button.Active = true |
09 | elseif path.Status = = 1 then |
10 | button.TextColor 3 = Color 3. new( 255 , 255 , 0 ) |
11 | button.Active = true |
12 | elseif path.Status = = 2 then |
13 | button.TextColor 3 = Color 3. new( 255 , 0 , 0 ) |
14 | button.Active = false |
15 | end |