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
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
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.
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.
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.)
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