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