I’ve tried many methods and tested this myself but the Battle NPC does not want to simply stop me like it usually does… Any ideas?
local function findTarget(Range, NPC)
local ray = Ray.new(NPC.PrimaryPart.Position, NPC.PrimaryPart.CFrame.lookVector * Range) -- Calculating the raycast.
local hit = workspace:FindPartOnRay(ray, NPC) -- Creating a ray to see what the npc sees.
if hit then
for _, player in pairs(Players:GetPlayers()) do -- We're going to loop through all players to see if the npc sees any of them.
if player.Character and player.Character:IsAncestorOf(hit) then
return player.Character
end
end
end
end
function BattleNPCs.Wander(NPC)
if NPC:FindFirstChild("IsFocused") then
while NPC.IsFocused.Value == false do
local Prim = NPC.PrimaryPart
local Config = NPC.Config
local GoalPosition = Prim.Position + generateRandomDestination()
local Path = SimplePath.new(NPC)
Path.Visualize = true
Path:Run(GoalPosition)
while Path._status == "Active" do
local target = findTarget(Config.Range.Value, NPC)
if target and target.Humanoid.Health > 0 and target.Spotted.Value == false then
NPC.IsFocused.Value = true
Path:Stop()
print("BATTLE-NPC | TARGET LOCATED! EXITING PATROL MODE")
InitiateAttack(NPC, target)
FaceTarget(NPC, Prim, target)
break
end
task.wait(0.25)
end
end
end
end