Making Zombie gameplay better

Hello, I’m making a zombie game right now, the zombies go to you, damage you, and are decently smart, but the problem is that they can never catch up to you, even if they are faster. This is because my position is constantly changing, so every time they go to where they thought I was positioned, It turns out I’m like 6 studs away. I’m wondering if there is a way to make them catch up to you?

Script

local PathfindingService = game:GetService("PathfindingService")
local DebrisService = game:GetService("Debris")
local manager = require(script.Parent.ZombieManager)

game.Players.PlayerAdded:Wait()

function FindClosestPlayer(pos)
	local character = nil
	local closestDistance = math.huge
	local players = game.Players:GetChildren()
	for i=1, #players do
		local Character = players[i].Character or players[i].CharacterAdded:Wait()
		if (Character.PrimaryPart.Position - pos).Magnitude < closestDistance then
			closestDistance = (Character.PrimaryPart.Position - pos).Magnitude
			character = Character
		end
	end
	return character
end

function FindClosestDistance(pos)
	local character = nil
	local closestDistance = math.huge
	local players = game.Players:GetChildren()
	for i=1, #players do
		local Character = players[i].Character or players[i].CharacterAdded:Wait()
		if (Character.PrimaryPart.Position - pos).Magnitude < closestDistance then
			closestDistance = (Character.PrimaryPart.Position - pos).Magnitude
		end
	end
	return closestDistance
end

while wait(0.2) do
	local zombies = workspace.ZombieFolder:GetChildren()
	if #zombies > 0 then
		
		
		
		for i,v in ipairs(zombies) do
			
			zombies[i].Humanoid:LoadAnimation(zombies[i].Humanoid.Walk):Play()
			local follower = zombies[i]
			local Humanoid = follower.Humanoid
			local Distance = FindClosestDistance(follower.PrimaryPart.Position)
			local EndDestination = FindClosestPlayer(follower.PrimaryPart.Position)
			local path = PathfindingService:CreatePath()

			path:ComputeAsync(follower.HumanoidRootPart.Position, EndDestination.PrimaryPart.Position)
			if Humanoid.Health == 0 then
				DebrisService:AddItem(follower, 5)
				follower.Parent = workspace.DeadZombies
			else
				if path.Status == Enum.PathStatus.Success then
					
					local nodes = path:GetWaypoints()
					task.spawn(function()
						for i= 1,(#nodes - 1) do
							Humanoid:MoveTo(nodes[i+1].Position)
							if Distance < 5 then
								EndDestination.Humanoid.Health -= follower:GetAttribute("Damage")
							end
						end
					end)
				end
			end
		end
	end
end

If you need other things or have tips, please tell me. Thank you!

Link, Alpha Force - Roblox

1 Like
while wait(0.2) do

May cause lag but you’ll need to decrease this if you want the NPCs to move to a more frequently updated and thus more accurate position of the closest player’s character.

Ok, im gonna try this right now, Do you recommend doing while true do?

No, that’ll crash the client. Just decrease the value inside the parantheses.

If you want easy-to-use, smart AI pathfinding, I highly recommend the SimplePath module. I’ve used it in most all of my games, and have yet to find significant issues with it. It updates the path per waypoint, generating smoother and more realistic paths.

Devforum post: SimplePath - Pathfinding Module
Repository: Getting Started - SimplePath

Ok, sounds cool, but would it be able to catch up to a player?
Do you think I could put a lineforce or something in the rootpart to make it faster?

Yeah, it should be able to. It is basically using the “while” method, but connecting it to a module event (in my opinion, it is cleaner/more efficient).

It runs faster, but still won’t be able to catch up to the player.

What did you try?
task.wait() should yield the best results.

Ok, i’ll look into this. Thanks

I tried this but they still seem to lag, if you play the game you’ll see what i mean.

Problem solved! put the task.spawn function around the main piece of code and the zombies can catch up now, a little bit laggy, but worth it.