Script that waits until they are close;
repeat wait() until (part1.Position - part2.Position).magnitude <= 2
Script that waits three seconds;
wait(3)
What I would like to know is how to make it so that the script waits until the parts are close, but if they're still not close after three seconds, it will continue with the code anyways. I'm not sure where to start.
Simple version.
local maxDuration=3;--// The max time for it to wait. local minDistance=2;--// The min distance between the parts. local startTime=tick() + maxDuration; repeat coroutine.yield(); until ( Part1.Position - Part2.Position ).magnitude <= minDistance or startTime - tick() <= 0; --// Code continues.
How it works:
maxDuration
is the maximum amount of time it will wait.
minDuration
is the minimum amount of units the of the magnitude.
startTime
is the current time added by the max duration.
coroutine.yield()
is a faster version of wait( 0 )
.
startTime - tick()
is the time the loop started subtracted by the current time. If it is 0 or less, it will stop. (a countdown)
One way we could do it is something like a promise -- two things race to trigger the same thing and then come to a conclusion "together" (based on the results each gave) (in this case, it's just ignore the second one)
local isFirst = true function dothing(close) if isFire then if close then -- Parts were close before three seconds else -- Three seconds elapsed first end end isFirst = false end spawn(function() wait(3) dothing(false) end); repeat wait() until (one-two).magnitude < 2 dothing(true);
Usually I wouldn't opt for this approach. I would try to write them in one loop:
local stopTime = tick() + 3; while wait() do if (one - two).magnitude < 2 then doCloseThing() break end if tick() > stopTime then doTimeThing() end end
local num = 3 -- Time You Want local close = false for i = 1,num do if (part1.Position - part2.Position).magnitude <= 2 then close = true break end wait(1) end --[[ Close = true if parts are close together after 3 seconds]]