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
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!