Zombie pathfinding

local function atk(Target, destination)
	local Value = Values.AttackPlayer.Value
	if Value == true then
		local Distance = (Model.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
		if Distance > 4 then
			local pathy = pathGet(destination)
			if pathy.Status == Enum.PathStatus.Success then
for i, v in pairs (pathy:GetWaypoints()) do 
 
					Humanoid:MoveTo(v.Position)
				end

			else
				print("Path Unsuccesful")
			end

		else	

			Target.Humanoid.Health -= 15 -- I had changed the lookvector here after it attacks and made it so it jumps, now it should work with NO problem.
		end
	else
		warn("Value not true")
	end
end

Here it should work with no problems now

1 Like

yeah, thats working, but only 1 thing, i have humanoid in this truck part, how i can damage it when zombie finished moving to the truck?

1 Like

Do you want to damage the humanoid or the truck?

1 Like

i have humanoid inside the truck and i want to damage it and show on HealthBarUI
image

1 Like

I mean when zombie has no target, it moves and attacks the truck

1 Like

image_2024-03-02_004507568 Copy what’s inside of HealthGUI

				local Distance = (Model.HumanoidRootPart.Position - Truck.PrimaryPart.Position).Magnitude
				if Distance > 5 then
	
				else
					local dmg = 30
					while wait(1) do
						task.delay(1, function()
							Truck:FindFirstChild("Humanoid"):TakeDamage(dmg)
							HealthValue.Value = HealthValue.Value - dmg
							HealthText.Text = HealthValue.Value
							print(HealthValue.Value)
							print(HealthText.Text)
						end)
						if targetFind() then
							break
						end
					end
				end
		

Final script would be

local Model = script.Parent
local Humanoid = Model.Humanoid
local PSS = game:GetService("PathfindingService")
local Path = PSS:CreatePath()
local Values = script.Parent.Configs
local blockedConnection
local nextWaypointIndex = 1
local reachedConnection
local Truck = game.Workspace:FindFirstChild("Map").Truck
local HealthValue = Truck:FindFirstChild("HealthGUI").HealthBarUI.HealthValue
local HealthText = Truck:FindFirstChild("HealthGUI").HealthBarUI.HealthAmount
local function seeTarget(Target)
	local origin = Model.PrimaryPart.Position
	local direction = (Target.PrimaryPart.Position - Model.PrimaryPart.Position).unit * 90
	local ray = Ray.new(origin, direction)

	local hit, pos = workspace:FindPartOnRay(ray, Model)

	if hit then
		if hit:IsDescendantOf(Target) then
			return true
		end--
	else
		return false	
	end--
end
local function targetFind()
	local Players = game.Players:GetPlayers()
	local MaxDistance = 50 -- use any
	local GetPart 
	local firstTarget
	for i, plr in pairs(Players) do
		if plr.Character then
			local Target = plr.Character
			local Distance = (Model.PrimaryPart.Position - Target.PrimaryPart.Position).Magnitude
			if Distance < MaxDistance then 
				firstTarget = 	Target
				--print(Target.Name)
				MaxDistance = Distance
			end
		end
	end
	return firstTarget
end
local function getPath(destination)
	local PathTable = {
		AgentHeight = 2,
		AgentRadius = 1,
		AgentCanJump = false,
		AgentCanClimb = true,
		Costs = {
			Jump = 1,
			Climb = 1,
			Danger = math.huge
		}
	}
	local Path = PSS:CreatePath(PathTable)

	Path:ComputeAsync(Model.HumanoidRootPart.Position, destination)
	return Path

end
local function pathGet(Destination)
	local target = targetFind()
	local PathTable2 = {
		AgentHeight = 2,
		AgentRadius = 1,
		AgentCanJump = true,
		AgentCanClimb = true,
		Costs = {
			Jump = 1,
			Climb = 1,
			Danger = math.huge
		}
	}

	local Path2 = PSS:CreatePath(PathTable2)
	Path2:ComputeAsync(Model.PrimaryPart.Position - Vector3.new(0, Model.PrimaryPart.Size.Y/0.75, 0), target.PrimaryPart.Position)

	--if PathWaypoint.Action == Enum.PathWaypointAction.Jump then
	--	Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	--end

	return Path2
end

local function atk(Target, destination)
	local Value = Values.AttackPlayer.Value
	if Value == true then
		local Distance = (Model.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
		if Distance > 4 then
			local pathy = pathGet(destination)
			if pathy.Status == Enum.PathStatus.Success then
				for i, v in pairs (pathy:GetWaypoints()) do
	
					Humanoid:MoveTo(v.Position)
				end

			else
				print("Path Unsuccesful")
			end

		else	

			Target.Humanoid.Health -= 15

		end
	else
		warn("Value not true")
	end
end

local function moveTo(destination)
	local path = getPath(destination)
	if path.Status == Enum.PathStatus.Success then

		for i, v in pairs (path:GetWaypoints()) do

			if v.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			local Target = targetFind()
			if Target and Target.Humanoid.Health > 0 then
				--print("Found", Target.Name,"now targeting")
				atk(Target)
				--print(path.Status)
				Values.AttackPlayer.Value = true			
				break 
			else
				blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
					if blockedWaypointIndex >= nextWaypointIndex then
						-- Stop if there's a part
						blockedConnection:Disconnect()
						-- Re compute
						moveTo(destination)
					end
				end)

				--print(path.Status)
				Values.AttackPlayer.Value = false
				local Distance = (Model.HumanoidRootPart.Position - Truck.PrimaryPart.Position).Magnitude
				if Distance > 5 then
	
				else
					local dmg = 30
					while wait(1) do
						task.delay(1, function()
							Truck:FindFirstChild("Humanoid"):TakeDamage(dmg)
							HealthValue.Value = HealthValue.Value - dmg
							HealthText.Text = HealthValue.Value
							print(HealthValue.Value)
							print(HealthText.Text)
						end)
						if targetFind() then
							break
						end
					end
				end
				--print("No target.", "Moving to", v.Position)
				Humanoid:MoveTo(v.Position)
				Humanoid.MoveToFinished:Wait()
			end
		end
	else
		print("Path Unsuccesful")
		--Humanoid:MoveTo(destination.Position - (Model.HumanoidRootPart.CFrame.LookVector * 15))
	end


end
local function Move()

	local path = Truck.PrimaryPart
	local point = path.Position
	wait()
	--print(point)
	moveTo(point)
end

while task.wait() do
	Move()
end
2 Likes

You don’t really need a humanoid in the part, just because you wanted it that way.

2 Likes

Thats working, thank you so much! :smiley:

2 Likes

Glad, if you’re concerned about the zombie lagging back then you can use NetworkOwnership

:smile:

I wrote an entire script in this post, please don’t ask people to write entire systems for you in the future!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.