How to make the zombie stop without it finishing

Hi, I’m having a problem with my script. The zombie doesn’t see me when it’s moving. it only sees me when it’s standing still. It has something to do with the humanoid.MoveToFinished:Wait(), But i don’t have a solution.

Script:

local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local zombies = CollectionService:GetTagged("Zombies")

local viewRange = 30
local fieldOfView = 70

local objectLastDetected = nil

local function getCharacters()
	local characters = {}

	for index, player in Players:GetPlayers() do
		table.insert(characters, player.Character)
	end

	return characters
end

local function raycast(zombie, start, finish)
	local parameters = RaycastParams.new()

	parameters.FilterDescendantsInstances = {zombie, getCharacters()}
	parameters.FilterType = Enum.RaycastFilterType.Exclude

	local raycast = workspace:Raycast(start, (finish - start), parameters)

	if raycast then
		return true
	else
		return false
	end
end

local function checkSight(zombie)
	local detectable = {}

	local characters = getCharacters()
	local taggedObjects = CollectionService:GetTagged("Detectable")

	for index, character in characters do
		table.insert(detectable, character)
	end

	for index, object in taggedObjects do
		table.insert(detectable, object)
	end

	for index, object in detectable do
		if object:IsA("Model") then
			object = object.PrimaryPart
		end

		local headPosition = zombie.Head.Position

		local objectPosition = object.Position
		local headCFrame = zombie.Head.CFrame

		local direction = (objectPosition - headPosition).Unit
		local lookVector = headCFrame.LookVector

		local dotProduct = direction:Dot(lookVector)
		local angle = math.deg(math.acos(dotProduct))

		local distance = (headPosition - objectPosition).Magnitude

		if angle > fieldOfView then
			continue
		end

		if distance > viewRange then
			continue
		end

		if raycast(zombie, headPosition, objectPosition) then
			return
		end

		if object:IsA("Model") then
			objectLastDetected = object.Parent
		else
			objectLastDetected = object
		end

		return true
	end
end

local function followPath(zombie, destination)
	local humanoid = zombie:FindFirstChildOfClass("Humanoid")
	local path = PathfindingService:CreatePath()
	
	local success, errorMessage = pcall(function()
		path:ComputeAsync(zombie.PrimaryPart.Position, destination.Position)
	end)
	
	if success and path.Status == Enum.PathStatus.Success then
		for index, waypoint in path:GetWaypoints() do
			
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
end

local function attack(zombie)
	print(objectLastDetected)
end

local function patrol(zombie)
	local waypoints = workspace.Waypoints:GetChildren()
	local randomNumber = math.random(1, #waypoints)
	
	followPath(zombie, waypoints[randomNumber])
end

for index, zombie in zombies do
	local humanoid = zombie:FindFirstChildOfClass("Humanoid")

	if not zombie:IsA("Model") then
		continue
	end

	if not humanoid then
		continue
	end

	local function checking()
		while task.wait(1) do
			if checkSight(zombie) then
				attack(zombie)
			else
				patrol(zombie)
			end
		end
	end

	coroutine.wrap(checking)()
end
2 Likes

Are you sure this isn’t working? I tried the same script you provided and it did work. Does your zombie not have the Zombies tag attached to it? Do you not have a Waypoints folder in your workspace? What does the console say?

The problem is that the zombie doesn’t see me when it’s walking only when it’s standing still.

I tried the exact same script and it ended up working for me, can you try this again? Can you read my original reply? It asked various questions, like do you have a Zombies attached to the zombies? Do you have a Waypoints folder in your workspace? Does you console/output have any errors?

I found out the problem. The humanoid.MoveToFinished:Wait() stops everything until the zombie reaches it’s destination. That’s why when the zombie stops the zombie can see me again, it’s because the zombie reached it’s destination.

It’s weird that it worked for you

The reason is because :Wait() is an event that will yield (essentially just means stop) the script until said event is fired. Meaning that if the event has to take 10 seconds to fire, it’ll stop the script’s execution for 10 seconds, to fix this instead make it an event connection, like

humanoid.MoveToFinished:Connect(function()

end)

etc, and it should work!

I’m sorry I got your original question wrong, my new reply should be a fix to it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.