Configuration for Ai when Distance is (120)

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)

I think you need to use the Dot property on vectors for this.

– Magnitude Ai Code –

------------------------------------------------------------------------------ Magnitude Ai
-- Storages
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Player
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local PlayerGui = Player.PlayerGui

-- Root (Player & AI)
local PlayerRoot = HumanoidRootPart
local AIRoot = workspace.AI["Mr. Twigs"]:WaitForChild("HumanoidRootPart")

-- Static 
local StaticOverlay = Random.new()
local Cache = os.clock()

-- GUI
local MagnitudeGui = PlayerGui:WaitForChild("MagnitudeGui")
local StaticEffect = MagnitudeGui.StaticEffect

-- Sounds
local SoundsFolder = ReplicatedStorage.Sounds
local StaticSound = SoundsFolder.Static
local HeartbeatSound = SoundsFolder.Hearbeat

-- Sound Settings
local OriginalVolume = StaticSound.Volume
local OriginalPlaybackSpeed = StaticSound.PlaybackSpeed
StaticSound.Volume = 0 -- Default Volume
StaticSound.PlaybackSpeed = 0 -- Default Playback Speed
StaticSound:Play()

-- Settings
local FrameRate = 60 -- In Points per tick; Density of Static
local FrameRateEnabled = true -- Enabled 
local MaxDistance = 120 -- In Range; Max Distance between Player & Ai (80)
local MaxVolume = 10 -- In Volume; Max Volume (10)
local MaxPlaybackSpeed = 0.055 -- In Playback Speed; Min Playback Speed (0.01)

-- Static Control
game:GetService("RunService").RenderStepped:Connect(function()
	if not FrameRateEnabled or os.clock() - Cache > 1 / FrameRate then
		Cache = os.clock()
		StaticEffect.Position = UDim2.fromScale(StaticOverlay : NextNumber (-1, 0), StaticOverlay : NextNumber (-1, 0))
	end
end)

-- Magnitude Distance Control (Distance & Volume)
while task.wait() do
	local Distance = (PlayerRoot.Position - AIRoot.Position).Magnitude
	local Scale = math.max(0, 1 - (Distance / MaxDistance))
	StaticEffect.ImageTransparency = 0.95 - Scale
	StaticSound.Volume = OriginalVolume * MaxVolume * Scale 
	StaticSound.PlaybackSpeed = OriginalPlaybackSpeed * MaxPlaybackSpeed * Scale
end

how may I able to do that, sorry?

Well I never read your post fully but I saw you wanted something to happen when the player looks at the bot or whatever.

I’m not sure how to use dot myself, so I’d recommend learning about it on the docs or somewhere. But I’ve heard it’s used for direction.

I feel like i may be missing something but couldn’t you use Magnitude on the CanSeeTarget function?, since the Fog is the part disallowing players from viewing him at that distance then it makes sense there to me

1 Like