NPC Stopped Working

Have an NPC in the game that finds closest player, follows them, and shoots at them. This NPC worked perfectly five days ago. But today it does not work at all. I have not changed the code at all. I loaded a version of the game from a week ago to test and the NPC works in THAT version, but when I cut and paste him into the current game it stops working. Something I did over the past week stopped the NPC from working.

I thought that maybe I had deleted a remote event or something like that, but no. The NPC does not use remote events. The only major change from the last week was that I added teams, but the NPC does not work in either of the two places in the game – one has teams and one does not.

So, maybe the players are not loading in because the NPC gets its list from the Players folder. To see if the players loaded in properly, I put in this script into workplace:

while true do
print(game.Players:GetPlayers())
end

All I got back was a long string of {} , {}, {}…

So I think the problem is that the players are no longer loading into the Players folder?? Is that even possible? How do I fix that?

Any idea what could be going on???

Here is the script for the NPC:

local NPC = script.Parent
local HRP = script.Parent:FindFirstChild(“HumanoidRootPart”)
local mouth = script.Parent.Mouth
local damage = 25
local lasersound = script.Parent.Torso.Laser
local PoPo = game.ReplicatedStorage.PoPoNPC

local function GetClosestPlayer(range)

local List = {}

Target = nil
for i, v in pairs(game.Players:GetPlayers()) do
	local dist = v:DistanceFromCharacter(HRP.position)
	if dist <= range then
		table.insert(List, {dist, v})
	end
end
	
table.sort(List, function(A, B)
	return A[1] < B[1]
end)

pcall(function()
	Target = List[1][2]
end)

return Target

end

local function shootplayer()
wait(2)
local laser = Ray.new(mouth.CFrame.p, (Target.Character.PrimaryPart.CFrame.p - mouth.CFrame.p).unit50)
local hitpart,hitposition = game.Workspace:FindPartOnRay(laser,Target,false,true)
local beam = Instance.new(“Part”, game.Workspace)
local debris = game:GetService(“Debris”)
beam.BrickColor = BrickColor.new(“Bright green”)
beam.FormFactor = “Custom”
beam.Material = “Neon”
beam.Transparency = 0.5
beam.Anchored = true
beam.CanCollide = false
local distance = (mouth.CFrame.p - hitposition).Magnitude
beam.Size = Vector3.new(.3,.3,distance
2)
beam.CFrame = CFrame.new(mouth.CFrame.p,hitposition)*CFrame.new(0,0,-distance)
debris: AddItem(beam,.2)
lasersound:Play()
if hitpart then
local hithumanoid = hitpart.parent:FindFirstChild(“Humanoid”)
if not hithumanoid then
hithhumanoid = hitpart.Parent.Parent:FindFirstChild(“Humanoid”)
end
if hithumanoid then
hithumanoid:TakeDamage(damage)
end
end
end

NPC.Humanoid.Died:Connect(function()

local ball = Instance.new("Part")
ball.Parent = game.Workspace
ball.Shape = Enum.PartType.Ball
ball.Size = Vector3.new(10,10,10)
ball.Transparency = 1
local parts = ball:GetTouchingParts()
parts.Anchored = false
wait(.1)
local explosion = Instance.new("Explosion")
explosion.Parent = game.Workspace
explosion.Position = NPC.Torso.Position
wait(3)
NPC:Destroy()
ball:Destroy()

end)

while true do
repeat
local Target = GetClosestPlayer(100)
wait(1)
if Target == nil then
end

until Target ~= nil
wait(.1)
NPC.Humanoid:MoveTo(Target.Character.PrimaryPart.Position)
shootplayer()
end

if NPC.Humanoid.Health == 0 then
wait(1)
NPC:Destory()
end

I think many yes the player thing s are not going into the right folder but if I was you I would double check and do a little test run and check if the right things are going where they are supposed to be going I hope this helped!:grin:

I found the problem. In case anyone has this problem in the future. The NPC was loading in before the character and so the NPC code was crashing when it found no players. Added a wait and now it works!