Why many people prefer GetPlayers() over getChildren() when finding players.GetPlayers() is dangerous if you want the character

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Some people says that getPlayers() is better

Explain the importance of GetPlayers() – dont copy from the defintion,i need the REAL REASON.

1 Like

Show me that you can get character of the player via GEtPlayers()

If can,prove it by testing

game.Players:GetPlayers()[playerName].Character

1 Like

The thing is is there any error

I don’t know, I’m at my home village celebrating Eid Ul fitr
Why don’t you test it yourself

If i test it,surely will have this error

(“HUMANOID”) IS NOT A VALID MEMBER OF PLAYER.CHARACTER(something like this

GetPlayers() is like game.Players:GetChildren() but only returns players.

Each player has a .Character, but that is only set when they spawn, so it can be nil and even if not you might have to WaitForChild(“Humanoid”) in case ot doesn’t exist yet.

Use the search feature and search the answers from the several other topics that you and others have made that answer this exact question. If it’s in the documentation, or if the answer is the same thing something many people are telling you over and over again, it’s what is correct.

6 Likes

Example:

local Players = game:GetService("Players")

local characterTable = {}
local function retrieveAllCharacters(player)
    local Character = player.Character or player.CharacterAdded:Wait()

    if Character then
        print("The Character of "..player.Name.." exists!")
        table.insert(characterTable, Character)

        local Humanoid = Character:WaitForChild("Humanoid")
        if Humanoid then
            print("The Humanoid of "..player.Name.."'s Character exists!")
            Humanoid.WalkSpeed = 100
        end
    end
end

local randomAmountOfTimeExample = math.random(5, 10)
print("Waiting "..tostring(randomAmountOfTimeExample).." seconds before continuing")
task.wait(randomAmountOfTimeExample)

for _, player in Players:GetPlayers() do
    retrieveAllCharacters(player)
end

warn(characterTable)

:GetPlayers, like :GetChildren, returns an array, you should use Players:GetPlayers()[1] or Players[playerName].

You know this is an entirely different problem from why :GetPlayers() is better than Players:GetChildren(), right?

Characters, and in extension, Humanoids, don’t load in immediately after a player joins the game - you have to wait for the character to actually be loaded in (mainly by using CharacterAdded).

GetPlayers() is safer than Players:GetChildren() since it only gets you players, and nothing else (on the very off chance you’ll add something other than players in your Players service).

See what I mean? You’re asking two very different things here, and I think this is just an xy problem.

Get to the point - what problem are you having exactly?

You prove to me by writing this code in a simulation game and show me that character can be found by printing

for _, Player in Players:GetPlayers() do
    print(`Player name: {Player.Name}`)
    
    local Character = Player.Character or Player.CharacterAdded:Wait()
    
    print(`Character of {Player.Name}'s name: {Character.Name}`)
end

? You aren’t making it clear what you want.

what is the point you are trying to make here

The reason to use GetPlayers over GetChildren, for whenever you want to fetch players, is that the script automatically exclude anything that isn’t a player object, that might be located inside the “players” tab. Look in your explorer, there is the “players” tab I am referering to.

If you want to use GetChildren over GetPlayers - feel free to do so. Some people also uses “game.workspace” over just doing “workspace”, the difference in this case is that your script might error if you use GetChildren, if you at some point have an object type of some sort inside the “players” tab, that isn’t a player object.
When does this happen? - Never, if you’ve not placed it there yourself.
But there is 0 reason to use GetChildren over GetPlayers in case of getting player objects, but there is a reason for GetPlayers being a thing in the first place - to prevent any errors.

Game play,put that code it,and see whether it can print the Character statment or not

I already know it can, but alright:

image

Also ever so slightly modified the script. The :GetPlayers part was not changed, though.

Replacing :GetPlayers with :GetChildren also resulted in the same output.

local Players = game:GetService("Players")

Players.PlayerAdded:Wait()

for _, Player in Players:GetPlayers() do
	print(`Player name: {Player.Name}`)

	local Character = Player.Character or Player.CharacterAdded:Wait()

	print(`Character of {Player.Name}'s name: {Character.Name}`)
end

However, if I then change the script to create a “fake player” that’s really just a StringValue, the following happens with :GetChildren:

With this code:

local Players = game:GetService("Players")

Players.PlayerAdded:Wait()

local FakePlayer = Instance.new("StringValue")
FakePlayer.Name = "I am a fake player"
FakePlayer.Parent = Players

for _, Player in Players:GetChildren() do
	print(`Player name: {Player.Name}`)

	local Character = Player.Character or Player.CharacterAdded:Wait()

	print(`Character of {Player.Name}'s name: {Character.Name}`)
end

But, as said before, using :GetPlayers in this case removes the error as the “fake player” is ignored. :GetPlayers only cares about real players, not StringValues. That is the only difference between Players:GetChildren and Players:GetPlayers.

1 Like