I am encountering difficulties with properly targeting specific players in my NPC script. The script is designed to identify and chase down the nearest player if they are in the TargetPlayers table. However, it seems to encounter issues when attempting to target and attack with these specific players, leading to unexpected behavior.
Upon investigation, I have observed that the script occasionally returns nil
for the nearest player, even when there are valid targets present. This unexpected behavior prevents the NPC from initiating the intended actions towards the target, causing disruptions in the gameplay experience, and soft-locking the entire server. Yeah.
I have thoroughly reviewed the script, including the targeting logic and pathfinding implementation, but have been unable to identify the root cause of this issue. I am seeking guidance and advice from the community to help me resolve this problem and ensure the NPC can properly target and interact with the intended players.
Here is my code for checking the nearest player:
local SCP096 = script.Parent.Parent
local PathfindingModule = require(SCP096.PathfindingModule)
local Pathfinder = SCP096
local function FindNearestPlayer()
if #shared.TargetPlayers ~= 0 then --// Make sure the table isn't empty
local found = nil
local last = math.huge
for _, Player in pairs(shared.TargetPlayers) do --// Get all of the TargetPlayers
local Character = Player.Character
if Character and Character.Humanoid.Health ~= 0 then --// Make sure their character exists and isn't dead
local distance = Player:DistanceFromCharacter(Character.HumanoidRootPart.Position)
if distance < last then
found = Player
last = distance
print("Nearest player found. Their name is", Player.Name) --// Debugging stuff
end
end
end
if found ~= nil then --// Everything works properly
shared.NearestPlayer = found
print("The nearest player is", last, "away. Their name is", found.Name)
local Goal = found and found.Character and found.Character.HumanoidRootPart
return Goal
else --// Breaks the entire NPC
print("No nearest player found.")
return false
end
end
end
while true do
local Path = PathfindingModule.new(Pathfinder)
while true do
local Goal = FindNearestPlayer()
if Goal ~= false then --//Pathfind
Path:Run(Goal)
end
end
end
Here are the “errors” i get:
It prints “No nearest player found.” until roblox literally force stops the script. This happens very rarely, and I have no idea what is causing it. Any help is appreciated.
Script that adds player to TargetPlayers table
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("Viewed")
local SCP096 = game.Workspace:WaitForChild("SCP-096")
local IsLookingBoolean = SCP096.IsLooking
IsLookingBoolean.Value = false
shared.TargetPlayers = {}
RemoteEvent.OnServerEvent:Connect(function(Player)
if not table.find(shared.TargetPlayers, Player) then
local TargetBoolean = Instance.new("BoolValue")
TargetBoolean.Name = "Target"
TargetBoolean.Value = true
TargetBoolean.Parent = Player.Character
table.insert(shared.TargetPlayers, Player)
IsLookingBoolean.Value = true
end
end)