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

Multiple Destinations [closed]

Asked by
RSoMKC 45
11 years ago

I'm learning how to do basic AI programming. I found this script on this website which I'm really appreciative of:

1Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(10,0,0), Workspace.Baseplate )
2-- ??
3Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(10,0,10), Workspace.Baseplate )

My question is: how do I tell an AI to move to a different destination after it reached it's first destination, without it cutting off the first string?

0
None of the answers received worked out with at least 4 destinations. Two of the scripts had the same mistake of pointing the script to the wrong direction, as well. local torso = humanoid.Parent & local humanoid = workspace["Test AI"] RSoMKC 45 — 11y

Locked by JesseSong

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

6 answers

Log in to vote
3
Answered by
nate890 495 Moderation Voter
11 years ago

Use magnitude and repeatedly check if the Torso of the AI is close to the destination. To do this, you need to use a repeat loop:

1local AI = game.Workspace["Test AI"]
2local destinations = {Vector3.new(10,0,0), Vector3.new(10,0,10)}
3 
4for i, vector in pairs(destinations) do
5    AI.Humanoid:MoveTo(vector, Workspace.Baseplate)
6    repeat wait() until (AI.Torso.Position-vector).magnitude < 4
7end

The number 4 can be changed to your preference. Test it out and tell me how it goes, I'm very rusty so there's bit of a chance that it may not work.

Ad
Log in to vote
2
Answered by
MrNicNac 855 Moderation Voter
11 years ago

It's best to simply make a clean, organized function to "wait" for a humanoid to reach its destination. You can use this:

01function WaitUntilAtDestination(humanoid)
02    local torso = humanoid.Parent
03    if torso.Parent then
04        repeat wait() until (torso.Position - humanoid.TargetPoint).magnitude <= 2.5
05    end
06end
07 
08Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(0,0,0), Workspace.Baseplate )
09WaitUntilAtDestination( Workspace['Test AI'].Humanoid )
10Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(10,0,10), Workspace.Baseplate )

It's easy to use as many times as you need:

01Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(0,0,0), Workspace.Baseplate )
02 
03WaitUntilAtDestination( Workspace['Test AI'].Humanoid )
04 
05Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(13,5,10), Workspace.Baseplate )
06 
07WaitUntilAtDestination( Workspace['Test AI'].Humanoid )
08 
09Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(11,0,0), Workspace.Baseplate )
10 
11WaitUntilAtDestination( Workspace['Test AI'].Humanoid )
12 
13Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(32,0,5), Workspace.Baseplate )
Log in to vote
2
Answered by
Unclear 1776 Moderation Voter
11 years ago

If you're doing some basic AI work with humanoids that can move with the MoveTo method, you can try using the Running event to help you out with having tasks happen only after a task has been completed.

The Running event in Humanoid fires every time the Humanoid begins walking or stops walking. This is pretty good for using in conjunction with the MoveTo method, particularly because it has the added bonus of returning the speed of the Humanoid at the time the event is being fired. If the speed is 0, that means the Humanoid has stopped moving. In this case, this also means that it's time to start up the next task!

Here's some example code of an AI moving between two points over and over again...

01local humanoid  = workspace["Test AI"] -- The humanoid of our AI
02local iterator  = 1 -- Keep track of which position to use
03local positions = { -- Hold all of the position-part pairs
04    {Vector3.new(10, 0, 0), workspace.Baseplate},
05    {Vector3.new(10, 0, 10), workspace.Baseplate}
06}
07 
08humanoid.Running:connect(function(speed)
09    if speed == 0 then -- If the humanoid has stopped running...
10 
11        if positions[iterator + 1] == nil then -- Looping back to the beginning of the table when at the end
12            iterator = 1
13        else -- Every other instance
14            iterator = iterator + 1
15        end
View all 21 lines...
Log in to vote
1
Answered by
Merely 2122 Moderation Voter Community Moderator
11 years ago

To determine when the AI reaches a destination, you can compare the distance from its torso to a point in the 3D world.

01local torso = Workspace['Test AI'].Torso
02 
03local destination = Vector3.new(10, 0, 0)
04Workspace['Test AI'].Humanoid:MoveTo( destination, Workspace.Baseplate )
05 
06local closeEnough = 5 --studs
07 
08while (torso.Position - destination).magnitude > closeEnough do
09    wait(1)
10end

Then you can set a new destination.

Log in to vote
1
Answered by
MunimR 125
11 years ago

Have something like

1Destination=Vector3.new(10,0,0)
2Workspace['Test AI'].Humanoid:MoveTo( Destination, Workspace.Baseplate )
3repeat wait(1)until (Workspace['Text AI'].Torso.Position-Destination).magnitude <= 2)
4Destination=Vector3.new(10,0,10)
5Workspace['Test AI'].Humanoid:MoveTo( Destination, Workspace.Baseplate )

I don't know if this will work, but it's what came from the top of my head.

Log in to vote
-2
Answered by 11 years ago

I was thinking that you could say the 'if the ai's position is equal to that position, then' thing.