Zombie-Script not working

Hello! I’m trying to make a Zombie script where the Zombie follows the nearest target.
I don’t get any errors and everything prints out but it doesn’t follow me though.
Don’t worry, I enabled R6 so there is a Torso.

local zombieModel = script.Parent
local Hum = zombieModel.Humanoid
local zombieTorso = zombieModel.Torso
local AggroDistance = 200


local function SearchForTarget()
	for _,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("Humanoid") ~= Hum then
			print("Hum Found which is not the Zombie")
			--Distance
			if (v.Torso.Position - zombieTorso.Position).magnitude < AggroDistance then 
				print("Distance check")
				--New Aggrodistance + Follow player
				AggroDistance = (v:FindFirstChild("Torso").Position - zombieTorso.Position).magnitude 
				print("New Aggrodistance set")
				return v:FindFirstChild("Torso") --Returns the position of the target
				end
			end
		end
end

--Game loop

while wait(1) do
	print("GameLoop")
	local Target = SearchForTarget() --Gets the returned value (Torso.Position)
	if Target then
		print("Target is not nil") --If its not nil
		Hum:MoveTo(Target.Position) 
		print("Moves to Target")
	end
end

Is your zombie anchored? 30Chars

1 Like

Yep. image

Unanchor everything except for the HumanoidRootPart

1 Like

Done, he fell out of the baseplate somehow, lol.

Turn on can collide for the root part.

1 Like

Also use HumanoidRootPart instead of Torso.

1 Like

It’s the same as Torso. TheDevKing made this and it works. I wanted to recreate it without looking at his script.
His Script:

local ZombieTorso = script.Parent.Torso
local ZombieHumanoid = script.Parent.Humanoid


local function findTarget()
    local agroDistance = 100
    local target = nil
    for i,v in pairs(game.Workspace:GetChildren()) do
        local human = v:FindFirstChild("Humanoid")
        local torso = v:FindFirstChild("Torso")

        if human and torso and v ~= script.Parent then

            if (ZombieTorso.Position - torso.Position).magnitude < agroDistance then  --checks the distance



            agroDistance = (ZombieTorso.Position - torso.Position).magnitude --Sets new agrodistance, so if someone else is closer, it follows him
            target = torso               --Sets the target, because target was nil before
        end
    end
    end

    return target

end


while wait(1) do
    local torso = findTarget()            --torso will be nil, as its the same as target so if torso is not nil< then do something
    if torso then
        ZombieHumanoid:MoveTo(torso.Position)
    end
end```
2 Likes

Is this from TheDevKing lololol
EDIT OH IT IS

That’s cool, he does some good tutorials around scripting for beginners and advanced

1 Like

Yep, I want to recreate it without looking at this script. I’m actually done with all the TheDevKing tutorials so I wanted to make something that he made before bc it’s easier to compare our scripts like that and see what I did wrong.

1 Like

Right maybe its because you havent added the else where the randomly generated movement is supposed to go. If it doesnt detect a torso it won’t move.

ZombieHumanoid:MoveTo(ZombieTorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50))
1 Like

That doesn’t work.

(30chars)

It should be as an else in the while wait(1) loop

1 Like

Why is the end red underlined by the way??

1 Like

It doesn’t work. Script:

local zombieModel = script.Parent
local Hum = zombieModel.Humanoid
local zombieTorso = zombieModel.Torso
local zombieHumanoidRootPart = zombieModel.Hum
local AggroDistance = 200


local function SearchForTarget()
	for _,v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("Humanoid") ~= Hum then
			print("Hum Found which is not the Zombie")
			--Distance
			if (v.Torso.Position - zombieTorso.Position).magnitude < AggroDistance then 
				print("Distance check")
				--New Aggrodistance + Follow player
				AggroDistance = (v:FindFirstChild("Torso").Position - zombieTorso.Position).magnitude 
				print("New Aggrodistance set")
				return v:FindFirstChild("Torso") --Returns the position of the target
				end
			end
		end
end

--Game loop

while wait(1) do
	print("GameLoop")
	local Target = SearchForTarget() --Gets the returned value (Torso.Position)
	if Target then
		print(Target.Parent) --If its not nil
		Hum:MoveTo(Target.Position) 
		print("Moves to Target")
		
	else
			zombieTorso:MoveTo(ZombieTorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50))
			
	end
end

image

1 Like

Alright, I found the solution. It was the Zombie Model’s fault. I changed it and now and TheDevKing’s code works. His code didn’t work on the old Zombie aswell. Thank you.

1 Like

Please HumanoidRootPart even if you are R6. HumanoidRootPart exists for all R6 and R15 avatars.

1 Like

Need an extra closing bracket at the end. Glad you solved it! Free Model scripts will often conflict or interfere with your own so be sure to remove them.

1 Like