Problem with zombie breaking boards

hey again, helpful devforum. i’ve configured my zombie’s code to attempt to break down boards that are in the way of it’s path to the player. it’s not doing too well, though. dunno how to explain it.


the zombie only starts the path to the boards when i walk infront of the boards. the zombie doesnt have any special eyesight mechanisms. just magnitude.

-- main NPC behavior loop with path obstruction handling
local function startNPCBehavior()
	while npcHum.Health > 0 do
		local closestPlayer = getClosestPlayer()

		if closestPlayer then
			if not isChasingPlayer then
				isChasingPlayer = true
				npcHum.WalkSpeed = speed
				playSound(spottedSound)
				print("Player spotted!")
			end

			-- Check for a blocking board
			local blockingBoard = getBlockingBoard(closestPlayer.HumanoidRootPart.Position)

			if blockingBoard then
				-- Attempt to move towards the blocking board
				local pathSuccess, path = pcall(function()
					return path:Run(blockingBoard.Position)
				end)

				if not pathSuccess or not path then
					print("Path to board blocked, unable to reach.")
				else
					print("Moving towards blocking board at:", blockingBoard.Position)
					-- Damage the board if within range
					if (hrp.Position - blockingBoard.Position).Magnitude <= DAMAGE_RANGE then
						local boardHealth = blockingBoard:GetAttribute("Health")
						if boardHealth then
							blockingBoard:SetAttribute("Health", boardHealth - DAMAGE_AMOUNT)
							local sound = damageSounds[math.random(1, #damageSounds)]
							playSound(sound)
						end
					end
				end
			else
				-- Move towards the player if no blocking board is detected
				local pathSuccess, path = pcall(function()
					return path:Run(closestPlayer.HumanoidRootPart.Position)
				end)

				if not pathSuccess or not path then
					print("Path to player blocked, unable to reach.")
				else
					print("Chasing player at position:", closestPlayer.HumanoidRootPart.Position)
				end
			end
		else
			if isChasingPlayer then
				isChasingPlayer = false
				npcHum.WalkSpeed = wanderSpeed
				playSound(lostSound)
				print("Player lost..")
			end
			-- Wander behavior when no player is detected
			local wanderPosition = hrp.Position + Vector3.new(math.random(-20, 25), 0, math.random(-20, 25))
			local pathSuccess, path = pcall(function()
				return path:Run(wanderPosition)
			end)

			if not pathSuccess or not path then
				print("Path to wander position blocked, unable to reach.")
			else
				print("Wandering to position:", wanderPosition)
			end
			task.wait(wanderDelay)
		end

		applyGroupRepulsion()
		damageNearbyTargets()  -- Call the range-based damage function

		task.wait(0.1)
	end

im using SimplePath module btw. lmk if you need the full script. tysm :slight_smile:

It is probably due to it not being able to find a path until you step infront of it, meaning its probably an issue with the module.

i havent used this module myself so im not sure of any solutions, neither am i the greatest at pathfinding.
try messing with the modules code, or maybe contacting the original developer of the module if you cant.

like i said beforehand, its most likley due to it not being able to find a diret oath until you step infront of the planks, try printing whether it has a path or not, and/or marking the current path it has.

sorry that i couldnt be of much help

1 Like

Looking at the code, it may also be an issue with finding the board, does it use raycasts?
if so, thats the issue.

  1. it dosent see a path to the player when behind wall
  2. it only sees the board if the player is infront of it due to raycasting

(these two only apply if your raycasting)

you may need to expand the details you share, including the getboard function

1 Like

yeah it uses raycasts for getting boards. thanks for your suggestions

-- Function to detect blocking boards in the zombie's path
local function getBlockingBoard(targetPosition)
	local direction = (targetPosition - hrp.Position).Unit
	local ray = Ray.new(hrp.Position, direction * rangeToChase)
	local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, {npc, workspace.Zombies})
	if hitPart and hitPart:GetAttribute("Built") == true and hitPart:GetAttribute("Health") then
		return hitPart
	end
	
	return nil
end
-- Function to get the closest player
local function getClosestPlayer()
	local closestPlayer = nil
	local shortestDistance = rangeToChase
	for _, player in pairs(players:GetPlayers()) do
		local character = player.Character
		if character and character:FindFirstChild("HumanoidRootPart") and player.Team == aliveTeam then
			local distance = (character.HumanoidRootPart.Position - hrp.Position).Magnitude
			if distance < shortestDistance then
				closestPlayer = character
				shortestDistance = distance
			end
		end
	end
	return closestPlayer
end

thats most likely the issue then then a racast wouldnt get the board unless it directly hits it

something i’d try is getting the closest board set to the player its attempting to chase and then have it pathfind to that if there isnt a clear path

though that would cause it to go a longer way around rather then straight through ther board if you do specificly what i said

another thing you can do is jsut pathfind to the player with the pathfinding ignoring the boards,
and when the zombie gets close, then starts breaking the boards

1 Like

so i found out how to make zombies ignore the boards (inserting a pathfinding modifier into the boards), but the zombies try and jump on top of the boards now. how can i fix this? is there a better way to make them ignore the boards?

im not good with pathfinding myself, but maybe try using collision groups?

1 Like

the problem is that simplepath has an option where if its “stuck” it will jump, i also believe certain path params tell the npc to jump as part of a generated path, in this case, its likely the stuck contingency playing.

1 Like

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