So I want to do a thing where if you’re in the monsters Canseetarget, they won’t stop chasing even though you’re out of their sight
mostly like the mimic and other horror games
yes its gnomecode
but how would i make it so that the monster will continously chase the player when they’re spotted from the said monster
local Overlord = script.Parent
local Humanoid = Overlord.Humanoid
local PathfindingService = game:GetService("PathfindingService")
Overlord.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = Overlord.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Overlord.HumanoidRootPart.Position) .unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Overlord)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 50
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Overlord.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 17,
["AgentRadius"] = 5,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Overlord.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Overlord.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 13 then
Humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local attackAnim = Humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = 0
end
end
function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print("Target Found", target.Name)
attack(target)
break
else
print("Moving too", waypoint.Position)
Humanoid:MoveTo(waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
end
else
Humanoid:MoveTo(destination.Position - (Overlord.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while true do
wait(0.1)
patrol()
end