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

Using an event for a Repeat Until?

So I've got this bit,

		repeat
			distance = (point - script.Parent.Torso.Position).magnitude
			wait()
		until script.Parent.Humanoid.MoveToFinished or distance < 4

Now, I know that it's an event, but based on what I'm doing I sort of want it in this repeat. I'm using it for a deviation of the stuff on the Pathfinding wiki page, but it moves on after you're within a certain distance of a node, which isn't working well for me, I'd like it to stop after it finishes the moveto - Is there a way to use an 'event'-type thing in a repeat until? Or a workaround?

As you can see I'm also using it with a distance so using a function to do this would just be more clunky overall. How it currently works, is that the pathfinding script will derp around as if it's finishing moving far earlier than it should be, zig-zagging but not moving around objects, which is very weird as it implies for some reason the MoveToFinished is firing off-time from when it actually stops moving.

1 answer

✓ Accepted answer

This is a bit of a hybrid. It will first establish the event, then go through the repeat loop until either the event is fired or the distance is calculated to be less than 4, and finally it disconnects the event.

local eventFinish = false
local event = script.Parent.Humanoid.MoveToFinished:connect(function()
	eventFinish = true
end)
repeat
	distance = (point - script.Parent.Torso.Position).magnitude
	wait()
until eventFinish or distance < 4
event:disconnect() -- once repeat loop finishes the event is disconnected, whether it was fired or not

7 comments

0

Just updated my question, sorry, does this still apply?

0

In that case you would have to use a loop.

0

I'm a bit confused. Use a loop? Example?

0

Loops in scripting or things like for, while, and repeat. I have updated my answer with a bit of a hybrid to work for both circumstances now.

0

That works, thanks, but weirdly that seems to not even be the part broken - the real reason I think it's not moving is, for some unknown reason, my While True Do isn't repeating. Can I PM you and get some 1-on-1 help?

1

Sure, I'll just need to change my inbox settings first.

0

Alright. PMed. Thanks for all your help.

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