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

how do i make a path finding zombie?

this will be long but im in need of some help

local Humanoid = script.Parent:WaitForChild("Humanoid")
local RootPart = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local Attack = script.Parent:WaitForChild("Attack")
local lowerTorso = script.Parent:WaitForChild("LowerTorso")
local AttackAnim = Humanoid:LoadAnimation(Attack)
AttackAnim.Priority = Enum.AnimationPriority.Action
local clone = script.Parent:Clone()
function walkRandomly()
	local xRand = math.random(-50,50)
	local zRand = math.random(-50,50)
	local goal = RootPart.Position + Vector3.new(xRand,0,zRand)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(RootPart.Position, goal)
	local waypoints = path:GetWaypoints()
	if path.Status == Enum.PathStatus.Success then
	for _, waypoint in ipairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then Humanoid.Jump = true
			
		end
		Humanoid.MoveTo(waypoint.Position)
		Humanoid.MoveToFinished:Wait(1)
		local timeout = Humanoid.MoveToFinished:Wait(1)
		if not timeout then print("STUCK")
			Humanoid.Jump = true
			walkRandomly()
		end
	end
	else
		print("PathFailed")
		wait(1)
		walkRandomly()
	end
end

function findPath(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(RootPart.Position,target.Position)
	local waypoints = path:GetWayPoints()
	
	if path.Status == Enum.PathStatus.Success then
		for_, waypoint in ipairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid.Jump = true
			end
			Humanoid:MoveTo(waypoint.Position)
			local timeout = Humanoid.MoveToFinished:Wait(1)
			if not timeout then Humanoid.Jump = true
				print("Too Long")
				findPath(target)
				break
			end
			if checkSight(target) then
				repeat 
					print("GoingToTarget")
					Humanoid:MoveTo(target.Position)
					Attack(target)
					wait(0.1)
					if target == nil then
						break
					elseif target.Parent == nil then
						break
					end
				until
				checkSight(target) == false or Humanoid.Health == 1 or target.Parent.Humanoid.Health == 0
				break
			end
			if (RootPart.Position - waypoints[1].Position).magnitude > 20 then
				print("Target Moved, generating new path")
				findPath(target)
				break
			end
		end
	end
	function checksight(target)
		local raything = Ray.new(RootPart.Position, (target.Position - RootPart.Position).Unit * 40)
		local hit,position = workspace:FindPartOnRayWithIgnoreList(raything, (script.Parent))
		if hit then
			if hit:IsDescendantOf(target.Position) and math.abs(hit.Position.Y - RootPart.Position.Y) < 3 then
				print("I can see target")
				return true
			end
		end
		return false
	end
	
	function findTarget()
		local distance = 100
		local target = nil
		local potentialtargets = {}
		local seetargets = {}
		for i,v in ipairs(workspace:GetChildren()) do
			local human = v:FindFirstChild("Humanoid")
			local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
			if human and torso and v.Name ~= script.Parent.Name then
				if (RootPart.Position - torso.Position).magnitude < dist and human.Health > 0 then
					table.insert(potentialTargets,torso)
				end
			end
		end
		if #potentialtargets > 0 then
			for i,v in ipairs(potentialTargets) do
				if checkSight(v) then
					table.insert(seeTargets, v)
				elseif #seeTargets == 0 and (RootPart.Position - v.Position).magnitude < distance then
					target = v 
					distance = (RootPart.Position - v.Position).magnitude
				end
			end
		end
		if seeTargets > 0 then
			distance = 200
			for i,v in ipairs(seeTargets) do
				if (RootPart.Position - v.Position).magnitude < distance 
					target = v 
					distance = (RootPart.Position - v.Position).magnitude
			end
		end
	end
	
end
function attack(target)
	if (RootPart.Position - target.Position).magnitude > 5 then
		AttackAnim:Play()
		if target.Parent ~= nil then target.Parent.Humanoid:TakeDamage()
	end
	end
	function died()
		wait(5)
		clone.Parent = workspace
		game:GetService("Debris"):AddItem(script.Parent,0.1)
	end
	Humanoid.Died:Connect(died)
	lowerTorso.Touched:Connect(function(obj)
		if not obj.Parent:FindFirstChild("Humanoid") then
			Humanoid.Jump = true
	end)
	
function main() 
	local target = findTarget()
	if target then
		Humanoid.WalkSpeed = 16
		findPath(target)
	else
		Humanoid.WalkSpeed = 8
		walkRandomly()
	end
end
while wait(0.1) do
	if Humanoid.Health < 1 then
		break
	end
	main()
end

1 answer

✓ Accepted answer

The script that you made is long and is very complicated. I prefer watching this video.It is hard to explain by typing.

https://www.youtube.com/watch?v=gSYx6MVa9Tc&list=RDCMUCqtTgteHiP9WkbvYA_ApjZg&index=5

4 comments

0

ok ty

0

wait it doesnt work id just like confirmation that you've used is maybe i just typed it wrong

0

Make sure you typed the distance correctly.I made that error when I used that script.

0

ok

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