|
Hello, How would I make a Npc/AI follow a path, Like find path/nodes and then use path finding to reach points?
Many thanks
Catts |
|
|
That is exactly what i have been trying to figure out for the past three days.... |
|
|
Iv been trying for the past week or two, Looking on YT videos but had no luck :/ |
|
BlueSquibJoin Date: 2016-04-03 Post Count: 108 |
|
|
|
Iv tried reading it but I'm so confused, What would be the simplest method to getting blocks that form a grid and insert them into a table and from that table check to see if a path is possible to finish point? :/ |
|
|
pathfinding computes a path for the npc.
if the npc is a humanoid then use walkto, in the direction of the path.
There is multiple videos as well.
watch them
breathe them
die for them. |
|
|
YAY, i did it, i made the npc move where i click, is this what you are trying to accomplish? thats what i was. If you want the code ill paste it here, i just took the wiki code and changed some lines to fit my game. |
|
|
|
Naa I have a grid of parts, In a square and I need the NPC to find a path by only stepping on these parts to reach the goall. But well done and thanks for your help! |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
local grid = {}
local finish = FinishPart --The part where u will finish
local current = Start --Part where u start
local path = {}
local function checkNext()
local score = math.huge
local part
for x, v in pairs(grid) do
for z, p in pairs(v) do
local dist = (current.Position - p.Position).Magnitude + (finish.Position - p.Position).Magnitude
if dist < score then
score = dist
part = p
end
end
return part
end
for i, v in pairs(Parts) do --Assuming u placed a table of all the parts here
grid[v.Position.X] = grid[v.Position.X] or {}
grid[x][v.Position.Z] = v
end
repeat
local current = checkNext()
path[#path + 1] = current
wait()
until current == finish
Try this. |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
Btw, forgot to add another end before
return part
and to use this, just loop through the table "path" like so
for i, v in pairs(path) do
and then just use the positions to set the way, like so
Humanoid:MoveTo(v.Position) --Assuming Humanoid is defined
I hope u get the point. |
|
|
Okays, Thankyou for your help!! So the script should look like this all put together?
Parts = {}
Humanoid = game.Workspace.Guest.Humanoid
for i, v in pairs(game.Workspace:GetChildren())do
if v.Name == "Node" then
table.insert(Parts, v)
end
end
local grid = {}
local finish = workspace.Finish --The part where u will finish
local current = workspace.Start --Part where u start
local path = {}
local function checkNext()
local score = math.huge
local part
for x, v in pairs(grid) do
for z, p in pairs(v) do
local dist = (current.Position - p.Position).Magnitude + (finish.Position - p.Position).Magnitude
if dist < score then
score = dist
part = p
end
end
end
return part
end
for i, v in pairs(Parts) do --Assuming u placed a table of all the parts here
grid[v.Position.X] = grid[v.Position.X] or {}
grid[x][v.Position.Z] = v
end
repeat
local current = checkNext()
path[#path + 1] = current
wait()
until current == finish
for i, v in pairs(path) do
Humanoid:MoveTo(v.Position)
end |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
Yes, I assume u have a part called "Start" and a part called "Finish"? |
|
|
Yeahh iv added them,, I keep getting a error though...
Workspace.Script:33: attempt to index field '?' (a nil value)
18:04:30.004 - Stack Begin
Script 'Workspace.Script', Line 33
Stack End
Line 33 is this,
grid[x][v.Position.Z] = v
Any ideas as to what could be causing this? :s |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
My bad. Replace this
grid[x][v.Position.Z] = v
with this
grid[v.Position.X][v.Position.Z] = v
P.S. I forgot to mention that u don't need the grid making function, u can just use the table with the parts. I forgot am not using another method I know off XD
(Though don't change it if u don't know how) |
|
|
Okayys haha, Thankyouu! :)
Its not erroring up anymore however its not doing anything, Like the Humanoid isn't moving |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
Maybe it's just cos the humanoid has no proper joints, or maybe the walkspeed is set to 0, or maybe the humanoid is anchored. |
|
|
Naa just checked all of that, I added print output after every segment to see how far the script runs to and it gets to
repeat
local current = checkNext()
path[#path + 1] = current
wait()
until current == finish
and then doesnt continue after this to move the humanoid, hmmm :S |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
Might be cos "current" never gets to be the finish :/
Near the top write up a loop to check which node is nearest to the finish and then in the checkNext() function write up an if statement that would tell the script to set current as finish if current is that nearest node.
I'm busy right now. |
|
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
Back. Sry, I forgot to tell u to remove a node from the table once it's been selected, cos that could be the reason for the script failing. |
|
|
Which section would I remove the node from, After the Humanoid has moved to it or after the grid[x][v.Position.Z] = v :S
Many thanks for your help! |
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
Parts = {}
Humanoid = game.Workspace.Guest.Humanoid
for i, v in pairs(game.Workspace:GetChildren())do
if v.Name == "Node" then
table.insert(Parts, v)
end
end
local grid = {}
local finish = workspace.Finish --The part where u will finish
local current = workspace.Start --Part where u start
local path = {}
local function checkNext()
local score = math.huge
local part
for x, v in pairs(grid) do
for z, p in pairs(v) do
local dist = (current.Position - p.Position).Magnitude + (finish.Position - p.Position).Magnitude
if dist < score then
score = dist
part = p
end
end
end
grid[part.Position.X][part.Position.Z] = nil
return part
end
for i, v in pairs(Parts) do --Assuming u placed a table of all the parts here
grid[v.Position.X] = grid[v.Position.X] or {}
grid[x][v.Position.Z] = v
end
repeat
local current = checkNext()
path[#path + 1] = current
wait()
until current == finish
for i, v in pairs(path) do
Humanoid:MoveTo(v.Position)
end |
|
|
Thankyouu! Just tried it and im getting this output error,
- Workspace.Script:28: attempt to index local 'part' (a nil value)
- Stack Begin
- Script 'Workspace.Script', Line 28 - local checkNext
- Script 'Workspace.Script', Line 38
- Stack End
Line 28 is this,
grid[part.Position.X][part.Position.Z] = nil
:/
|
|
KapKing47Join Date: 2012-09-09 Post Count: 5522 |
See after this line
if dist < score then
type this
print(part) |
|