So I’ve been working on a new pathing AI for an NPC which I done before as well as used the Player Service with no issue.
What is the issue?
However with this specific script for reasons I don’t know what. The *Players or GetPlayers() is not working. I will send some screenshots for examples.
Test code have used that isn’t working
This is where the script is Placed in the NPC model. Not sure if this could be an issue.
I’ve tried to search for an issue like what I’ve been having and found none. The script can compute everything else through my tests but not the Player service.
If anyone has any knowladge around this then help would be grately appreciated. And if you need anymore reference then let me know.
I gave this a try with task.wait(1) before the getplayers but I’ve gotten the same error I get from adjusting the code Averilongnem gave me which is this:
as @DoorsPro_Bacon said, the code is likely running before any players are in the game.
what is your script for?
if you want to loop through all players to eventually find the closest one to the npc, then you should run your code in a forever loop, e.g
while task.wait() do
local Players = game:GetService("Players"):GetPlayers()
for _, player in pairs(Players) do
print(player.Name)
-- do whatever you want here
end
end
You are completely correct that I am using this code for finding the closest player to the NPC
I have used this code before for a different NPC but at the moment I can’t get it to work in another script.
I tried your solution but it still isn’t printing anything in the outliner so I’m unsure if it is working or not yet
Ok it looks like this code you have given me works. It recongises that there is a player in the Players service as it prints 1 in the outliner.
tested it with print(player.Name) as well and it’s working. It looks like you and @DoorsPro_Bacon were right in the player hasn’t loaded in yet.
Somewhere I think I am running the code before loading the players in as have been having this issue with every new find closest player function I make.
I will see if I can use this to fix my error and thank you ever so much!!