How do I make a zombie that will follow and get hurt by not just one player?

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)

The script didn’t work:


I used my alt, it doesn’t follow still.

It also doesn’t follow me:

Oh, you’re using R15 (OKAY THEN)

Alright try this:

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
1 Like

It still seems like it isn’t working :thinking:


Really? Is there anything on your Output that you can check? (Press Ctrl + F9 or type in /console)

Developer Console In Game:


Output In Studio:

I’m not sure if they meant to write zombieHuman instead of zombieHum, try changing all instnaces of zombieHum to zombieHuman

I mistyped a couple of lines dang it

Ok try again

@EmbatTheHybrid I SWEAR I NOTICED THAT 30 SECONDS BEFORE

Now all we need to do is solve this problem and hopefully the zombie starts moving.
ice_screenshot_20210414-112425

May I see your full script to see what could be fixed

Zombie-Game (1).rbxl (37.4 KB)

This is the whole game so you can see all the scripts in it.

Change the script in the zombie to this

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

This will detect you up till 1000 studs

Thanks, you fixed all the problems I had. :+1::+1::+1:

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

If it errors when the zombie dies, you may need to add anotehr check in the while loop to check if t he zombie still exists

while zombieHuman and zombieHuman.Health > 0 do