Ive been working an AI for a horror game. In its dormant state it goes to the parts in a specified folder. when a player gets within a certain distance from the AI, it will chase it, and onces its in a closer distance it will kill the player. that part is pretty much figured out. but id love some help on fixing my script, where when the AI doesnt see the player but it still needs to get to the player, it doesnt just walk into a wall – a straight ray from the AI to the player rather than using pathfinding.
how can i make the script so when the AI no longer sees the player, it goes to its last known positions instead of just going straight to it – or even using some kind of pathfinding? im not sure
here is the script
local owl = script.Parent
local humanoid = owl.Humanoid
local d1 = false
wait();
owl.PrimaryPart:SetNetworkOwner(nil)
local canattack = true
local waypoints = workspace.Folder:GetChildren()
local atac = false
local footstp = script.Parent.Torso:WaitForChild("footsp")
local static = script.Parent.Torso:WaitForChild("sttc")
local scream = script.Parent.Torso:WaitForChild("scream")
local teleemint = script.Parent.Torso:WaitForChild("Teleemit")
local reps = game:GetService("ReplicatedStorage")
local mink = false
local PMA = false
local function canSeeTarget(target)
local origin = owl.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - owl.HumanoidRootPart.Position).unit * 150
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, owl)
if PMA == true then
return true
end
if hit then
if hit:IsDescendantOf(target) then
PMA = true
return true
end
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 80
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (owl.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 PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 5,
["AgentRadius"] = 2,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(owl.HumanoidRootPart.Position, destination.Position)
return path
end
local arm = 0
local away = false
local function attack(target)
atac = true
if humanoid.Parent.Head.Face.Texture == "rbxassetid://12001830691" then
humanoid.Parent.Head.Face.Texture = "rbxassetid://14207345901"
PMA = true
end
local distance = (owl.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 10 then
mink = true
if static.Playing == false and scream.Playing == false then
local plr = game.Players:GetPlayerFromCharacter(target)
end
print("moveto")
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local player = game.Players:GetPlayerFromCharacter(target)
local anim = humanoid:LoadAnimation(script.Attack)
if canattack == true then
print("okaykilled")
canattack = false
arm = 0
atac = false
end
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
if footstp.Playing == false then
footstp:Play()
footstp.PitchShiftSoundEffect.Octave = 0.5
footstp.PlaybackSpeed = 0.4
end
--footstp.Playing = false
--footstp.TimePosition = 0
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Killed.Value == false and arm < 60 then
attack(target)
break
else
if mink == true then
mink = false
print("fired")
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
if not humanoid.MoveToFinished then
print("arm")
end
end
end
end
end
function patrol()
if humanoid.Parent.Head.Face.Texture == "rbxassetid://14207345901" and atac == false then
humanoid.Parent.Head.Face.Texture = "rbxassetid://12001830691"
end
humanoid.WalkSpeed = 26
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while task.wait(.05) and d1 == false do
patrol()
end
as well as a video of what the problem is:
