The Ai responds… when the Player looks at the scarecrow then the scarecrow would yield. However, if the Player looks away from the scarecrow then the scarecrow would chase the Player, unless the Player looks away from the scarecrow.
The issue is… If the Player losses sight of the due to the density of the fog then the scarecrow and STILL wouldn’t chase the Player. This makes the Ai easy to avoid and easier to complete.
I need the scarecrow to follow the Player once the distance between the Player and the scarecrow is = 120 by range distance.
I need help or support to develop the scarecrow so when the static has faded (which indicates if the scarecrow is near by) then the scarecrow will follow the nearest Player.
– Ai Code –
----------------------------------------------------------------------------- Ai
-- Services
local TweenService = game:GetService("TweenService")
-- NPC
local NPC = script.Parent
local NPCHumanoid = script.Parent:WaitForChild("NPC")
local NPCRoot = script.Parent:WaitForChild("HumanoidRootPart")
if not NPCRoot then
warn("HumanoidRootPart not found in AI model")
return
end
-- Tween Fade
local FadeTime = 3.5; -- In TweenInfo; Fade Time
-- Touch Kill Control
local function Kill(Humanoid)
Humanoid.Health = 0
NPC.HumanoidRootPart.Jumpscare:Play()
end
NPCRoot.Anchored = true
-- Gui Control
task.wait(45)
NPCRoot.Anchored = false
-- script.Parent.HumanoidRootPart.Snap:Play()
-- Getting the PlayerGui.
for _, Player in game:GetService("Players"):GetPlayers() do
if Player:FindFirstChildOfClass("PlayerGui") then
local NoticeGui = Player:FindFirstChild("PlayerGui"):FindFirstChild("NoticeGui")
local NoticeText = NoticeGui:FindFirstChild("NoticeText")
NoticeGui.Alert:Play()
NoticeText.Visible = true
task.wait(FadeTime)
TweenService:Create(NoticeText, TweenInfo.new(1.5), {TextTransparency = 1}):Play() TweenService:Create(NoticeText, TweenInfo.new(1.5), {TextStrokeTransparency = 1}):Play()
end
end
-- Old GetTarget()
--[[ Ai Control
local function GetTarget()
local TargetReturn
local KillDistance = 2
local DetectionDistance = math.huge
local ClosestDistance = math.huge
for _, Target in ipairs(game:GetService("Players"):GetPlayers()) do
if Target.Character and Target.Character:FindFirstChild("Humanoid") then
local Magniutde = (Target.Character:FindFirstChild("HumanoidRootPart").Position - NPC.HumanoidRootPart.Position).Magnitude
if Magniutde < DetectionDistance and Magniutde < ClosestDistance then
TargetReturn = Target.Character
end
end
end
if TargetReturn and (TargetReturn:FindFirstChild("HumanoidRootPart").Position - NPC.HumanoidRootPart.Position).Magnitude < KillDistance then
Kill(TargetReturn:FindFirstChild("Humanoid"))
end
return TargetReturn
end]]
-- Ai Control
local function GetTarget()
local DetectionDistance = math.huge
local KillDistance = 2.5
local TargetReturn = nil
for _, Target in ipairs(workspace:GetDescendants()) do
if Target:FindFirstChild("Humanoid") and Target:FindFirstChild("HumanoidRootPart") and Target.Name ~= NPCRoot.Parent.Name then
local TargetHumanoid = Target.Humanoid
local TargetRoot = Target.HumanoidRootPart
if TargetHumanoid.Health > 0 and (TargetRoot.Position - NPCRoot.Position).Magnitude < DetectionDistance then
DetectionDistance = (TargetRoot.Position - NPCRoot.Position).Magnitude
TargetReturn = Target
if DetectionDistance < KillDistance then
Kill(TargetHumanoid)
end
end
end
end
return TargetReturn
end
-- If Player has turned away
local function CanSeeTarget(Target)
if NPC.Head.Orientation.Y > Target:FindFirstChild("Head").Orientation.Y - 180 and NPC.Head.Orientation.Y < Target:FindFirstChild("Head").Orientation.Y + 180 then
return true
end
end
-- Movement Detection Control
task.spawn(function()
local CanWander
while task.wait() do
local MainTarget = GetTarget()
if MainTarget then
CanWander = false
if CanSeeTarget(MainTarget) then
NPC.NPC.WalkSpeed = 25 -- Chase Speed
NPC.NPC:MoveTo(MainTarget:FindFirstChild("HumanoidRootPart").Position)
else
NPC.NPC.WalkSpeed = 16 -- Patrol Speed
end
else
CanWander = true
end
if CanWander then
local RandomPosition = math.random(-350, 350)
local PositionToMove = NPC.HumanoidRootPart.Position + Vector3.new(RandomPosition, 0, RandomPosition)
NPC.NPC:MoveTo(PositionToMove)
NPC.NPC.MoveToFinished:Wait()
end
end
end)