generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Skip the wait time in a function?

Previous Thread :: Next Thread 
CustomCuber is not online. CustomCuber
Joined: 03 Jul 2015
Total Posts: 1147
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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
CustomCuber is not online. CustomCuber
Joined: 03 Jul 2015
Total Posts: 1147
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 is not online. 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
CustomCuber is not online. CustomCuber
Joined: 03 Jul 2015
Total Posts: 1147
09 May 2016 10:03 PM
@JC_h
Thank you, that worked!


Report Abuse
CustomCuber is not online. CustomCuber
Joined: 03 Jul 2015
Total Posts: 1147
09 May 2016 10:03 PM
The first thing you said worked, I didn't try what you said about spawn().


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image