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

How does AI Pathfinding work?

What is the limitations of this? How much do you need to know in order for it to work efficiently? What's the most efficient way to do this?

What I want to do is create AI's in one of my games that can locate where people are. How I was thinking of doing is creating paths for them to follow, Set paths that they can go on in order to attack people around each 'marker'. Each marker will represent where they can go with set lines made by each marker connecting. (Also is there any way to make it so they can only go 1 way on each line? for example a cliff you can't get back up, but you can jump down)

You don't need to explain things, but that would be nice, along with any wiki pages that would be helpful to read on this subject.

5 comments

0

Should I try explaining it, I could break it down how it goes to the player(Waiting for your answer)?

0

I do understand in theory how it works. I just don't understand how I could use them in different situations.

0

Ok i'll try explaining it.

0

Okay, that would be great.

0

I just finished q.q

2 answers

Well path finding is basically a way to get to point A to point B, also there is no "better method" out of all the methods, because they do the same thing.

So I went on studio's and took a zombie AI script and thought I could break it down trying to show you how an AI works, I guess you can make your own path find method but there all similar in someway.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Right Arm")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end

end

So that's the script now let's break it down.

Part

local list = game.Workspace:children()
	local torso = nil -- This is defining if the torso that it's trying to find is equal to nil.
	local dist = 1000 -- MaxDistance that it can find the Torso.
	local temp = nil -- Will later be explained and so is the bottom.
	local human = nil
	local temp2 = nil

Part2

for x = 1, #list do -- For the number of children of workspace and was defined in Part1
		temp2 = list[x]  -- Saying that Temp2 is equal to every child of workspace
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then -- Now it's only narrowing it down to models and if the child is not script's parent(zombie)

Part3(Final)

temp = temp2:findFirstChild("Right Arm") -- Finding right Arm of the player in workspace
			human = temp2:findFirstChild("Humanoid") -- Remember Temp2 is every model and a player's cahracter is a model the rest is self explanatory
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then -- If the player right are is not equal and the humanoid is not equal to nil and the humanoid's health is greater then 0
				if (temp.Position - pos).magnitude < dist then -- If the position of the players - zombie's magnitude is less then the dist
					torso = temp -- the torso now equals temp
					dist = (temp.Position - pos).magnitude -- the dist variable is now equal to zombie - player's magnitude.
				end
			end
		end
	end
	return torso -- returns it
end

Hope this help's.

2 comments

0

In this case, there would be 4 parts.. You missed the very end of the script that makes the zombie move to the player. And I do understand that script. What I don't understand is the algorithms that are in the wiki Adark provided. How the nodes are made, how to know what modes will be closed and open.

0

Oh mk

There's really no one answer to this question. It depends on how your maps are made, how your game plays (i.e. weapon design) and countless other things.

Your 'markers' are typically called Nodes, by the way.

Here are the four pathfinding pages on the wiki. I suggest you just go for it and ask for help here when you get stuck. :>

5 comments

0

Is there any order in witch I should read things?

0

Nope. Read them all, though, and attempt whichever method you think will work best for you.

0

Okay, i read through most of them, what it seems like is it's just how they move, but.. it doesn't explain it very well. (Or at least not to me.) It also doesn't tell me what the 'Cost' is for each Node, Am I able to set a cost for each node or is it already predefined based on the distance? (It seems like it just keeps going to another one that's the closest while the one it just came from turns

0

It's arbitrary, really. I typically use cost as the straight-line distance from the current node.

0

Would you be able to provide me an example how to use one of them? So I can see how it would work?

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