I looked at some youtube video on how to make a zombie and a gun that will hurt only the zombie. The only problem is the zombie will only follow and take damage to a person you assigned it to.
Zombie Code
> local char = game.Workspace:WaitForChild("YeetSnacks") --The player is the only one that can damage the zombie.
>
> local Zombie = script.Parent
> local zombieHuman = Zombie:WaitForChild("Zombie")
>
> local hitbox = Zombie:WaitForChild("HitBox")
>
> local DMG = 10 -- The damage done when any player touches the hitbox.
> hitbox.Touched:Connect(function(hit) --When any player touches the hitbox area.
> 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 - DMG
> hit:Destroy()
> if zombieHuman.Health <= 0 then
> _G.deadZombies = _G.deadZombies + 1
> Zombie:Destroy()
> end
> end
>
> end
> end)
>
>
> while wait(1) do
> zombieHuman:MoveTo(game.Workspace.YeetSnacks.HumanoidRootPart.Position) --Follows only one person
>
> end
Oof, cause you’re only programming the zombie to follow you alone
What you’d need to do, is every once in a while check for the nearest target by putting it through a loop
Here’s the basic way to do it:
function nearPersonPos(obj)
local closest = nil
local bestDist = nil
for _,v in pairs(game.Players:GetPlayers()) do
if v.Character then
local tors = v.Character:FindFirstChild("Torso")
if tors then
local dist = (tors.Position-obj.Position).magnitude
if not bestDist or dist<bestDist then
closest = v
bestDist = dist
end
end
end
end
return closest
end
while zombieHuman and zombieHum.Parent and zombieHuman.Parent:FindFirstChild("Torso") do
zombieHuman:MoveTo(nearPersonPos(obj) or zombieHum.Parent.Torso.Position)
wait(1)
end
(I just randomly snatched this from somewhere but this is what you’d do if you want the Zombie to chase other players)
function nearPersonPos(obj)
local closest = nil
local bestDist = nil
for _,v in pairs(game.Players:GetPlayers()) do
if v.Character then
local tors = v.Character:FindFirstChild("UpperTorso")
if tors then
local dist = (tors.Position-obj.Position).magnitude
if not bestDist or dist<bestDist then
closest = v
bestDist = dist
end
end
end
end
return closest
end
while zombieHuman and zombieHuman.Parent and zombieHuman.Parent:FindFirstChild("UpperTorso") do
zombieHuman:MoveTo(nearPersonPos(obj) or zombieHuman.Parent.UpperTorso.Position)
wait(1)
end
local Zombie = script.Parent
local zombieHuman = Zombie:WaitForChild("Zombie")
local root = Zombie:WaitForChild("UpperTorso")
local hitbox = Zombie:WaitForChild("HitBox")
local DMG = 10
hitbox.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human and hit.Parent.Name ~= "Zombie" then
human.Health -= 5
end
if hit.Name == "Bullet" then
zombieHuman.Health -= DMG
hit:Destroy()
if zombieHuman.Health > 0 then return end
_G.deadZombies += 1
Zombie:Destroy()
end
end)
function nearPersonPos()
local closest
local bestDist = 1000
for _,v in pairs(game.Players:GetPlayers()) do
local char = v.Character
local tors = char and char:FindFirstChild("UpperTorso")
if not tors then continue end
local dist = (tors.Position-root.Position).Magnitude
if dist >= bestDist then continue end
closest = tors.Position
bestDist = dist
end
return closest
end
while zombieHuman.Health > 0 do
zombieHuman:MoveTo(nearPersonPos() or root.Position)
wait(1)
end