Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

4 comments

0

do you mean the script will be running while it's counting down 3?

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.

0

repeat wait() until will do the code UNTIL the parts are close together. Basically forever until the parts are close.

0

I know.

3 answers

✓ Accepted answer

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)

1 comment

0

Yeah I think I'll use that instead of the while loop because it's shorter and I like repeat.

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

5 comments

0

What does the spawn() function do exactly? The wiki seemed unclear to me.

0

The spawn function acts like coroutine.wrap(f)() and coroutine.resume(coroutine.create(f)), spawning a function in another thread.

0

It runs the function its given in a separate coroutine -- so it essentially starts it running "at the same time"

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?

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.

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]]

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.