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

Mob pathfinding like minecraft zombies?

So I'm making a dungeon game, and I want there to be guards that you have to kill, i would just retexture the default roblox player tracking mob, but it pathfinds through walls, so it's not very good for what I'm wanting to do.

Basically I want it to pathfind like a Minecraft zombie (doesn't pathfind through walls), but I'm unsure of how to do so.

If anyone could maybe share how I would do that, I would greatly appreciate it.

1 answer

✓ Accepted answer

The following code is little sniplets from my npc George, hes quite advanced. You can find him in person here:

https://www.roblox.com/library/331847610/Its-George

He can also (or could) drive vehicles and did i mention he can also get travel sick! :P

lol anyway the basics of his path finding is below. Your more than welcome to download him and disect his brain (he's kinda used to me doing it! lol)

Code:

local Self 	= script.Parent	 -- Base 
local ME 		= Self.Humanoid -- My Brain! :D
local Path 	= {}			-- a Table that holds the points of our path
local CurrentPoint = 1		-- Current point on our path to our destination

local function computePathtoV3(Target)
	local PFS = game:GetService("PathfindingService")
	local path = PFS:FindPathAsync(Self.HumanoidRootPart.CFrame.p,Target,1024)
	return path:GetWaypoints()
end


-- wrap this in your target finder function/bit!
--[[
kinda like 
local Target = workspace.TGazza
local TargetPos = Target.HumanoidRootPart.Position
]]
myPos = TargetPos
Path = computePathtoV3(myPos)


-- Main loop for traveling along the found path...
local atEnd = false
while true do
	if(#Path > 0 and AtEnd == false) then
		local Dist = (Self.HumanoidRootPart.Position - Path[CurrentPoint].Position).magnitude
		Self.Humanoid:MoveTo(Path[CurrentPoint].Position)
		if(Dist < 3) then
			CurrentPoint = CurrentPoint +1
		end
	end
	if(CurrentPoint >= #Path) then
		AtEnd = true
		--// do stuff!
	end
	wait()
end

Each time your npc gets to the end of a path you would clear the Path table and resetting the path index (CurrentPoint ) to 1

aka:

Path = {}
CutrrentPoint = 1

Hope this helps! :)

4 comments

0

what line does the normal pathfinding start? all I can find seems like just vehicle stuff (in the full model you sent)

0

also the script you sent doesn't match anywhere in the model you sent me...

0

yeah the script i posted here is the path script section. Its in the Brain script inside him. the computePathtoV3 is at line 145 and look right down the bottom of the script for where he walks the path.

0

Just search for the varable "Path" and you'll see

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