of     2   
chevron_rightchevron_rightchevron_right

ilovecats969
#188482034Monday, May 02, 2016 9:30 PM GMT

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
tackykiller10000
#188482100Monday, May 02, 2016 9:31 PM GMT

That is exactly what i have been trying to figure out for the past three days....
ilovecats969
#188483367Monday, May 02, 2016 9:54 PM GMT

Iv been trying for the past week or two, Looking on YT videos but had no luck :/
BlueSquib
#188486117Monday, May 02, 2016 10:35 PM GMT

ilovecats969
#188512317Tuesday, May 03, 2016 10:45 AM GMT

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? :/
Deranged_User
#188513318Tuesday, May 03, 2016 11:41 AM GMT

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.
tackykiller10000
#188515608Tuesday, May 03, 2016 1:17 PM GMT

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.
Deranged_User
#188515659Tuesday, May 03, 2016 1:19 PM GMT

congrats.
ilovecats969
#188515714Tuesday, May 03, 2016 1:21 PM GMT

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!
KapKing47
#188519298Tuesday, May 03, 2016 3:33 PM GMT

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.
KapKing47
#188519437Tuesday, May 03, 2016 3:37 PM GMT

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.
ilovecats969
#188521102Tuesday, May 03, 2016 4:27 PM GMT

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
KapKing47
#188521822Tuesday, May 03, 2016 4:52 PM GMT

Yes, I assume u have a part called "Start" and a part called "Finish"?
ilovecats969
#188523146Tuesday, May 03, 2016 5:36 PM GMT

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
KapKing47
#188523455Tuesday, May 03, 2016 5:44 PM GMT

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)
ilovecats969
#188523675Tuesday, May 03, 2016 5:50 PM GMT

Okayys haha, Thankyouu! :) Its not erroring up anymore however its not doing anything, Like the Humanoid isn't moving
KapKing47
#188523746Tuesday, May 03, 2016 5:52 PM GMT

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.
ilovecats969
#188524214Tuesday, May 03, 2016 6:03 PM GMT

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
KapKing47
#188525451Tuesday, May 03, 2016 6:38 PM GMT

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.
ilovecats969
#188527941Tuesday, May 03, 2016 7:35 PM GMT

Ooh oakyyys
KapKing47
#188532361Tuesday, May 03, 2016 9:02 PM GMT

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.
ilovecats969
#188562947Wednesday, May 04, 2016 9:02 AM GMT

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!
KapKing47
#188574091Wednesday, May 04, 2016 5:12 PM GMT

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
ilovecats969
#188576173Wednesday, May 04, 2016 6:23 PM GMT

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 :/
KapKing47
#188576627Wednesday, May 04, 2016 6:36 PM GMT

See after this line if dist < score then type this print(part)

    of     2   
chevron_rightchevron_rightchevron_right