AI stop detecting player when they're standing still

So I’ve scripted an AI that would shoot at players when they’re in range but the raycast sometimes just stop working when they’re standing still

Clip

here’s what I did

local NPC = script.Parent
local ignorelist = {NPC}
local Debris = game:GetService("Debris")
local RS = game:GetService("ReplicatedStorage")
local BulletholesMod = require(RS:WaitForChild("BulletholeTEX"))
local BulletSoundsMod = require(RS:WaitForChild("BulletImpactSounds"))
local FXFolder = RS:WaitForChild("Effects")
local HitFXFolder = FXFolder.HitEffect
local canfire = true
local firing = false
local ignorelist = {NPC}

local statsmod = require(script.Parent.Stats)
local spreadIntensity = statsmod.Spread
local Range = statsmod.Range
local FireRate = statsmod.FireRate
local ROF = 60/FireRate
local origin = NPC.HumanoidRootPart

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = ignorelist

local function Bullethole(hit, position, normal)
	--irrelevant
end

local function fireshots(direction)
	--irrelevant
end

local function ShootPlayer(player)
	local playerCharacter = player.Character
	if not playerCharacter then
		return
	end

	local playerPart = playerCharacter:FindFirstChild("HumanoidRootPart")
	if not playerPart then
		return
	end

	local npcPart = NPC:FindFirstChild("Head")
	if not npcPart then
		return
	end

	local direction = (playerPart.Position - npcPart.Position).Unit
	local raycastResult = workspace:Raycast(npcPart.Position, direction * Range, params)

	if raycastResult and raycastResult.Instance == playerPart then
		print("NPC shot player", player.Name)
		firing = true
		fireshots(direction)
	else
		firing = false
		print("lost target")
	end
end

local function DetectPlayers()
	while true do
		for _, player in ipairs(game.Players:GetPlayers()) do
			if player.Character then
				local npcPart = NPC:FindFirstChild("HumanoidRootPart")
				if npcPart and (player.Character.HumanoidRootPart.Position - npcPart.Position).Magnitude <= Range then
					ShootPlayer(player)
				end
			end
		end
		wait()
	end
end

DetectPlayers()

Any help is appreciated!

Your raycast only detects the humanoid root part and not the arms, legs, and accessories.

You should try replacing it with another method such as :GetPlayerFromCharacters if you want the raycast to hit players only.

1 Like

Well I also want to NOT detect stuff like limbs because if my player’s arms went through wall the “AI” will shoot it

Then you can probably just add another check just for this specific scenario.

If it hits an arm or leg do a reverse raycast or the newer shape cast from the players humanoid root part towards the npc to see if the player is behind a wall.

1 Like

That would be very tiresome and hard for me since I’m pretty inexperienced (imo) and I don’t think I can do the double check raycast you suggested :smiling_face_with_tear:

Nevermind, I got desperate and asked ChudGPT and it tells me that I’m stupid and just have to replace some “or” with “and”

from

if descendant.Name ~= “Head” or descendant.Name ~= “Torso” or descendant.Name ~= “Humanoid” then

to

if descendant.Name ~= “Head” and descendant.Name ~= “Torso” and descendant.Name ~= “Humanoid” then

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.