NPC Character (Your destinations)

Hey there y’all! :wave:t2:

I’m trying to make a NPC character to go to the your destinations also known as the paths. I watched @.GnomeCode’s video to make it called “AI Character Pathfinding

The scripted in the first part of the video was working very normally, the character started to walk to the paths but while changing the script from the beginning to the end of the tutorial, the AI script started to stop working.

I tried to see If I didn’t see some errors or forgot to add some more details but none of them made the script work again. His NPC worked very well but mine don’t. Here’s the scripts from the video to the end:

The beginning script: (Also, the NPC is called Johnny)

local johnny = script.Parent
local humanoid = johnny.Humanoid

while true do
      humanoid:MoveTo(workspace.destination.Position)
      humanoid.MoveToFinished:Wait()
      humanoid:MoveTo(workspace.destination2.Position)
      humanoid.MoveToFinished:Wait()
end

And here’s the final changes of the AI script:

local humanoid = johnny.Humanoid

local PathfindingService = game:GetService("PathfindingService")

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 7,
		["AgentRadius"] = 5.2
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(johnny.HumanoidRootPart.Position, destination.Position)
	
	return path
end	

local function walkTo(destination)
	local path = getPath(destination)
	
	for index, waypoint in pairs(path:GetWaypoints)) do
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end
end

local function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum].Position)
end

while wait(0.5) do
	patrol()
end

Video with the first script:

robloxapp-20210401-0124151.wmv

And the video with the final changes:

robloxapp-20210401-0126277.wmv

(If it’s lagging, bugging or having bad quality in the video, it’s because of the Roblox Recorder that somehow looks like that I have a Nokia phone)

And I don’t know how If I need to make the first script and add some details like “while wait (0.5)…” to make the NPC work or need to delete stuffs that can make errors. If I need to change, I really don’t know what I need since I don’t understand script codes and your specific languages to program stuffs but if y’all can help me out with this, I would appreciate that very much!

Thank you all! :v:t2:

  • Juan
1 Like

I guess it can’t find a path because of how Pathfinding service works?

1 Like

Hey @JuanGamerPlayz_RBLX.
If you could provide more details on what is exactly wrong I’d love to help you, but “His NPC worked very well but mine don’t” isn’t much to go on.

Thanks.

1 Like

Editing your second script,
I don’t think this is the problem but instead of using

math.random

you should use

for i, v in pairs(waypoints) do
--code here
end
1 Like

So I should put like this?

for i, v in pairs(waypoints) do
(1, #waypoints)
end

Well @RepValor I put some videos so people can download it:

Video with the first script:

robloxapp-20210401-0124151.wmv

And the video with the final changes:

robloxapp-20210401-0126277.wmv

And basically the first one just shows the script working normally and the second one not working it when GnomeCode started changing the everything from the script AI of the NPC. The part where I said “His NPC worked very well but mine don’t”, I didn’t mean to said that my NPC was working in some parts (If you got this context), and yes not working at all.