Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to make a script wait until Part1 is close to Part2 -- OR three seconds max?

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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.

0
do you mean the script will be running while it's counting down 3? aquathorn321 858 — 9y
0
I mean that it will wait until the parts are close, BUT if they don't get close after three seconds, the code will stop waiting for them to be close. Perci1 4988 — 9y
0
repeat wait() until will do the code UNTIL the parts are close together. Basically forever until the parts are close. ObscureEntity 294 — 9y
0
I know. Perci1 4988 — 9y

3 answers

Log in to vote
1
Answered by
Diitto 230 Moderation Voter
9 years ago

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)

0
Yeah I think I'll use that instead of the while loop because it's shorter and I like repeat. Perci1 4988 — 9y
Ad
Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
0
What does the spawn() function do exactly? The wiki seemed unclear to me. Perci1 4988 — 9y
0
The spawn function acts like coroutine.wrap(f)() and coroutine.resume(coroutine.create(f)), spawning a function in another thread. Diitto 230 — 9y
0
It runs the function its given in a separate coroutine -- so it essentially starts it running "at the same time" BlueTaslem 18071 — 9y
0
IIRC, Spawn's created thread doesn't start running until the next time the current thread yields (I.e. wait()s). Is that true or am I confusing it with something else? adark 5487 — 9y
0
adark: Probably, because it's just a coroutine (not a true thread). Usually that distinction doesn't matter since the server would freeze if you were taking any time at all do to things before yielding. BlueTaslem 18071 — 9y
Log in to vote
-1
Answered by 9 years ago
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]]
0
Unfortunately that's not what I'm asking. I mean that it will wait until the parts are close, BUT if they don't get close after three seconds, the code will stop waiting for them to be close. Perci1 4988 — 9y

Answer this question