i want to make the monster scream when a player is near but it just screams constantly
local rootPart = script.Parent.Torso
local human = script.Parent.Ashersing
function findtarget()
local dist = 100
local target = nil
for i,v in pairs(workspace:GetChildren()) do
local humanoid = v:FindFirstChild("Humanoid")
local head = v:FindFirstChild("Head")
if humanoid and head and v.Name ~= "Ashersing" then
if (head.Position - rootPart.Position).Magnitude < dist and humanoid.Health > 0 then
dist = (head.Position - rootPart.Position).magnitude
target = head
script.Parent.Head.scream:Play()
end
end
end
return target
end
while wait(1) do
local head = findtarget()
if head then
local ray = Ray.new(rootPart.Position, head.Position - rootPart.Position)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
if hit.Parent:IsDescendantOf(head.Parent)then
human:MoveTo(head.Position)
else
path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head.Position)
points = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(points) do
human:MoveTo(v.Position)
human.MoveToFinished:wait(2)
if v.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
if (points[#points].Position - head.Position).magnitude > 15 then
break
end
end
else
human:MoveTo(head.Position)
end
end
end
end
end