How do i make it follow nearest player?

Hello there! I am in need of help. There is a script thats deticated to making me able to kill a zombie and the other way around and it will follow me till it kills me. The thing is… I want it to attack the nearest player not just me! Here is the script if you wanna help me.

local char = game.Workspace:WaitForChild(“2_P90”)

local zombie = script.Parent
local zombieHuman = zombie:WaitForChild(“Humanoid”)
local zombieHumanRootPart = zombie:WaitForChild(“HumanoidRootPart”)

local hitbox = zombie:WaitForChild(“HitBox”)

local damage = 30
hitbox.Touched:Connect(function(hit)
if hit.Parent then
local human = hit.Parent:FindFirstChild(“Humanoid”)
if human and hit.Parent.Name ~= “Zombie” then
human.Health = human.Health - 5
end

	if hit.Name == "Bullet" then
		zombieHuman.Health = zombieHuman.Health - damage
		hit:Destroy()
		if zombieHuman.Health <= 0 then
			_G.deadZombies = _G.deadZomb + 1
			zombie:Destroy()
		end
	end
end

end)

local ServerStorage = game:GetService(“ServerStorage”)
local Humanoid = ServerStorage.Zombie.Humanoid

while wait(1) do
Humanoid:MoveTo(game.Workspace[“2_P90”].HumanoidRootPart.Position)
end

1 Like

You would want to check all of the players positions and use that to calc the distance from the Zombie.
You will want to compare the current closest with the player currently being checked and by replacing it with the closest each time it will do exactly what you want.

Here’s a rough way:

local Players = game.Players:GetPlayers()
local TargetInfo = {nil,100000000}
for i = 1,#Players do
    local Dist = (Players[i].Character.Torso.Position - Zombie.Torso.Position).magnitude
    if Dist < TargetInfo[2] then
        TargetInfo = {Players[i],Dist}
    end
end

-- Target Info Will store the closest player, and the distance from zombie

So instead of this
while wait(1) do
zombieHuman:MoveTo(game.Workspace[“2_P90”].HumanoidRootPart.Position)
i do that?

You will still want to use the :MoveTo().
That was only finding the closest target.

In order to move to the closest target you would want to recheck the closest target every so often and use :MoveTo(TargetInfo[1].Character.Torso.Position) in order to make the Z walk to the player.

so it would be
zombieHuman:MoveTo(TargetInfo[1].Character.Torso.Position)
and all of that other script above it?

yes, that would find the closest player at the time. However you would want to repeat this in order to move to the player. The player can move and the closest player could also change.

And in order to make it keep repeating what would i do?

you would want to use a loop with certain conditions.
You would most likely want to do it the entire time the zombie is still alive.

So:

while Zombie.Health > 1 do
    local Players = game.Players:GetPlayers()
    local TargetInfo = {nil,100000000}
    for i = 1,#Players do
        local Dist = (Players[i].Character.Torso.Position - 
        Zombie.Torso.Position).magnitude
        if Dist < TargetInfo[2] then
            TargetInfo = {Players[i],Dist}
        end
    end
    Zombie:MoveTo(TargetInfo[1].Character.Torso.Position)
    wait(2) -- Change this to the refresh closest player faster/Slower
end

What this would do is repeat the code inside the whole time the zombie is alive.
If the health is below 1 it must be 0(Dead). If it is killed by a player or :Destroy is used on the zombie than the health will hit 0 which will stop the script from running.
if you want to add other conditions you would just have to add it in. You can use ‘break’ to stop loops from running.

What I just wrote would cover the complete basic movement. It does not pathfind or jump. (I purposely put a minor error or two so you will have to read and understand what is going on to find it.)

I tested it but it did not work. Now it says
Workspace.Zombies.Zombie.Zombie:21: attempt to perform arithmetic (add) on nil and number

This seems to be the only addition in your script. I would assume this is line 21 where there error is occurring.

I have not seen any _G defined so _G must be what is causing your error.

_G.DeadZombies needs to be defined before you can do math with it.

What you are trying to do is to assign it using itself. But it hasn’t been defined yet so anything using _G will be nil. You need to give _G a starting value

Now it says there is an error on line 37
zombie:MoveTo(TargetInfo[1].Character.Torso.Position)
and the error says
Workspace.Zombies.Zombie.Zombie:37: attempt to index nil with ‘Character’

That’s one of the errors I put in there. You will need to correctly get the players character. This I will not do for you.

I will however give you a hint that perhaps the char hasn’t loaded in yet.

1 Like

Should i delete the local char? The player is able to kill the zombie but the zombie doesent follow the player.

Plus i also get an error! attempt to index nil with ‘Player’