Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 01 Sep 2016 04:28 PM |
I'm trying to detect every part in the Workspace named "PlateLevi", and set them moving asynchronously. However, when there exists more than 1 part with the name PlateLevi in Workspace, the script returns an error, telling me that I cannot resume a dead coroutine. Any ideas on how I can work around this?
math.randomseed(tick())
local Levitate Levitate = coroutine.wrap(function(Brick) while wait() do local Origin = Brick.CFrame Brick.CFrame = Origin*CFrame.new(0,math.sin(tick()),0) end end)
for i,v in pairs(workspace:GetChildren()) do if v.Name == "PlateLevi" then Levitate(v) wait(math.random(1,10)/10) end end
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 01 Sep 2016 04:29 PM |
| The Origin variable is also outside of the while wait() do, that's fixed in the real script. |
|
|
| Report Abuse |
|
|
Monadic
|
  |
| Joined: 03 Aug 2016 |
| Total Posts: 731 |
|
|
| 01 Sep 2016 04:29 PM |
Trying to bring back things from the dead I see... smh |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2016 04:31 PM |
most efficient methods @OP:
1) create a new coroutine each time and run it 2) create a bindable event, connect the function to event.Event:connect(), and after the loop, use event:Fire() |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2016 04:34 PM |
the event should be the best method for you
local Levitate = function(Brick) wait(math.random(1,10)/10) while wait() do local Origin = Brick.CFrame Brick.CFrame = Origin*CFrame.new(0,math.sin(tick()),0) end end
local event = Instance.new("BindableEvent") for i,v in pairs(workspace:GetChildren()) do if v.Name == "PlateLevi" then event.Event:connect(function() Levitate(v) end) end end event:Fire() |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 01 Sep 2016 04:41 PM |
Is any detriment caused by redefining the coroutine each time?
math.randomseed(tick())
for i,v in pairs(workspace:GetChildren()) do if v.Name == "PlateLevi" then local Levitate Levitate = coroutine.wrap(function(Brick) local Origin = Brick.CFrame local offset = math.random(1,5)/10 while wait() do Brick.CFrame = Origin*CFrame.new(0,math.sin(tick()-offset)*10,0) end end) Levitate(v) wait(math.random(1,10)/10) end end
|
|
|
| Report Abuse |
|
|
|
| 01 Sep 2016 04:46 PM |
it causes lag
thats why i said use bindables |
|
|
| Report Abuse |
|
|
grot327
|
  |
| Joined: 15 Jun 2010 |
| Total Posts: 14 |
|
|
| 01 Sep 2016 05:21 PM |
^ wrong
In this scenario that couroutine can cause no memory leaks because it is dependent on Brick existing. As soon as Brick is removed, it will error and ROBLOX will clean it for you eventually. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
|
| 01 Sep 2016 05:22 PM |
yes it does cause lag
if you run about 100 coroutines it lags a lot more then if you run 1 event
it works on my midi player |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 05 Sep 2016 12:49 PM |
Is spawn() better than the bindable event?
|
|
|
| Report Abuse |
|
|
|
| 05 Sep 2016 01:26 PM |
local parts = { } local levitate = coroutine.wrap(function() while true do for idx = 1, len do parts[idx] = parts[idx].CFrame * CFrame.new(0, math.sin(tick()), 0) end wait() end end)()
for i,v in pairs(workspace:GetChildren()) do if v.Name == "PlateLevi" then parts[#parts + 1] = v wait(math.random(1,10)/10) end end
|
|
|
| Report Abuse |
|
|