I'm learning how to do basic AI programming. I found this script on this website which I'm really appreciative of:
Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(10,0,0), Workspace.Baseplate ) -- ?? Workspace['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?
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:
local AI = game.Workspace["Test AI"] local destinations = {Vector3.new(10,0,0), Vector3.new(10,0,10)} for i, vector in pairs(destinations) do AI.Humanoid:MoveTo(vector, Workspace.Baseplate) repeat wait() until (AI.Torso.Position-vector).magnitude < 4 end
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.
It's best to simply make a clean, organized function to "wait" for a humanoid to reach its destination. You can use this:
function WaitUntilAtDestination(humanoid) local torso = humanoid.Parent if torso.Parent then repeat wait() until (torso.Position - humanoid.TargetPoint).magnitude <= 2.5 end end Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(0,0,0), Workspace.Baseplate ) WaitUntilAtDestination( Workspace['Test AI'].Humanoid ) Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(10,0,10), Workspace.Baseplate )
It's easy to use as many times as you need:
Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(0,0,0), Workspace.Baseplate ) WaitUntilAtDestination( Workspace['Test AI'].Humanoid ) Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(13,5,10), Workspace.Baseplate ) WaitUntilAtDestination( Workspace['Test AI'].Humanoid ) Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(11,0,0), Workspace.Baseplate ) WaitUntilAtDestination( Workspace['Test AI'].Humanoid ) Workspace['Test AI'].Humanoid:MoveTo( Vector3.new(32,0,5), Workspace.Baseplate )
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...
local humanoid = workspace["Test AI"] -- The humanoid of our AI local iterator = 1 -- Keep track of which position to use local positions = { -- Hold all of the position-part pairs {Vector3.new(10, 0, 0), workspace.Baseplate}, {Vector3.new(10, 0, 10), workspace.Baseplate} } humanoid.Running:connect(function(speed) if speed == 0 then -- If the humanoid has stopped running... if positions[iterator + 1] == nil then -- Looping back to the beginning of the table when at the end iterator = 1 else -- Every other instance iterator = iterator + 1 end humanoid:MoveTo(positions[iterator][1], positions[iterator][2]) -- Move to the position in question end end) humanoid:MoveTo(positions[1][1], positions[1][2]) -- Trigger what we set up above
To determine when the AI reaches a destination, you can compare the distance from its torso to a point in the 3D world.
local torso = Workspace['Test AI'].Torso local destination = Vector3.new(10, 0, 0) Workspace['Test AI'].Humanoid:MoveTo( destination, Workspace.Baseplate ) local closeEnough = 5 --studs while (torso.Position - destination).magnitude > closeEnough do wait(1) end
Then you can set a new destination.
Have something like
Destination=Vector3.new(10,0,0) Workspace['Test AI'].Humanoid:MoveTo( Destination, Workspace.Baseplate ) repeat wait(1)until (Workspace['Text AI'].Torso.Position-Destination).magnitude <= 2) Destination=Vector3.new(10,0,10) Workspace['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.
I was thinking that you could say the 'if the ai's position is equal to that position, then' thing.
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?