Players:GetPlayers() not working in my script

What do you want to achieve?

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
Players Error

This is where the script is Placed in the NPC model. Not sure if this could be an issue.


What solutions have you tried so far?

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.

4 Likes

I think the script runs before the player fully loads in the game.
Add game.PLayers.PlayerAdded:Wait() or a task.wait(x) before getting the players

3 Likes

Thanks for some of the knowledge there. Your script has made an error pop up in the outliner which is a great start as it didn’t show anything before.

This is the error I have been given:

2 Likes

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:

It seems like there I am doing something wrong with the Players service itself :confused:

2 Likes

Nvm seems like you do need to use GetPlayers mb bro. I removed my post

3 Likes

hello,

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
3 Likes

A more optimised approach might just be doing this:

local players = game:GetService("Players")
repeat task.wait() until #players:GetPlayers() > 0

--code here

If you do need to run this every frame, use RunService.Stepped for the server. If I’m not wrong, it’ll run each frame of the physics simulation.

Otherwise, use RunService.Heartbeat. For more optimisation, if this task is memory heavy, you should look into Parallel Luau.

3 Likes

You are completely correct that I am using this code for finding the closest player to the NPC :slight_smile:

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 :confused:

2 Likes

It’s all good, thank you for trying to help though :smiley:

2 Likes
local players = game:GetService("Players"):GetPlayers()
for _, player in ipairs(players) do
	print(player.Name)
end

ipairs would be better here.

3 Likes

guy just try this:
for _, player in pairs(game.Players:GetChildren()) do
print(player.Name()
end

2 Likes

thats odd, it seems to be running fine in an empty baseplate

try running this and see what it prints

while task.wait() do
    local Players = game:GetService("Players"):GetPlayers()

    print(#Players) 
end
3 Likes

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

2 Likes

you could just try Players.PlayerAdded:Wait() (unless you’ve already tried that)

edit: nvm, mark your post as solved so people like me don’t reply

1 Like

No worries. Thanks for the help though.

It does seem to the a Wait() issue as you said :smiley:

1 Like

Using playeradded event is the simpler alternative.

2 Likes

Looks like one of them scripts that would benefit from the 1st line being task.wait(?)
Take about 3 seconds to load the game when published.