|
| 09 May 2016 09:44 PM |
I made this script that's supposed to make a group of rock pillars rise up at the same time:
----------------------------
function raise(part) wait() print(part.Position) local newPos = part.Position + Vector3.new(0,207,0) local debounce = false local Diff = newPos - part.Position local Mag = Diff.magnitude local Direction = CFrame.new(part.Position,newPos).lookVector
if debounce then return end debounce = true for n = 0, Mag, .5 do part.CFrame = part.CFrame+(Direction*.5) wait((1/Mag)*.5) end debounce = false end
function moveParts() local parts = script:GetChildren() for i = 1,20 do raise(parts[i]) end end
script.Parent.button.ClickDetector.MouseClick:connect(moveParts)
---------------------
However, only one part rises at a time since it waits for the raise() function to complete before calling it again. How can I get around this and make all of them raise at the same time?
|
|
|
| Report Abuse |
|
|
|
| 09 May 2016 09:53 PM |
First of all, you initialize your debounce variable in your function to false, so it will ALWAYS be false.
Secondly, you can do this two ways. Your first option (easiest) is to Spawn() your raise function in the i loop. The second option is to rewrite your raise function in such a way that for each n, loop through the i parts and apply the change to all of them. |
|
|
| Report Abuse |
|
|
JC_h
|
  |
| Joined: 06 May 2016 |
| Total Posts: 9 |
|
|
| 09 May 2016 09:58 PM |
In order to perform multiple events at the same time, you will need to use a coroutine.
The following code is an example of a coroutine (1) local newThread = coroutine.create(function() print("hola") end) coroutine.resume(newThread)
To edit this to work with what you have, it may look a little like
function raise(part) -- your stuff end
function moveParts() local parts = script:GetChildren() for i = 1,#parts do local newThread = coroutine.create(function() raise(parts[i]) end) coroutine.resume(newThread) end end
(1) code obtained from http://wiki.roblox.com/index.php?title=Beginners_Guide_to_Coroutines#How_to_create_a_coroutine |
|
|
| Report Abuse |
|
|
|
| 09 May 2016 10:00 PM |
@AgentFirefox I tried just doing "spawn(raise(parts[i]))" and it didn't work, so I tried searching it on the wiki and I didn't understand. Can you explain further?
|
|
|
| Report Abuse |
|
|
JC_h
|
  |
| Joined: 06 May 2016 |
| Total Posts: 9 |
|
|
| 09 May 2016 10:02 PM |
If I understand it correctly, Spawn is not able to directly take arguments. The example on the wiki (1) is spawn(function() myfunc(a,b,c) end)
So, perhaps try this instead: spawn(function() raise(parts[i]) end)
(1) http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#Spawn |
|
|
| Report Abuse |
|
|
|
| 09 May 2016 10:03 PM |
@JC_h Thank you, that worked!
|
|
|
| Report Abuse |
|
|
|
| 09 May 2016 10:03 PM |
The first thing you said worked, I didn't try what you said about spawn().
|
|
|
| Report Abuse |
|
|