For i, v in pairs doesent work

for some reason, this script doesn’t work. it won’t even print. i don’t know what’s wrong. any help is appreciated.

Script:

for i, player in pairs(game.Players:GetPlayers()) do
	if player.Character then
		print(player.Name)
	end
end

2 Likes

Where is this script located? Like is it in ServerScriptService, StarterPlayerScripts, etc…

3 Likes

its located in serverscriptservice.

1 Like

Well since it’s a serverscript, it’s gonna be running before any clients have even joined.

Since that’s the case, it only goes through the players once. Even if a player has joined when its through the loop, the character might not.

I’d use ChildAdded() to tell when a player is added to Players, as well as use Player.CharacterAdded() to tell when a character spawns in

Hope I helped :slight_smile:

4 Likes

i tried this:

for i, player in pairs(game.Players:GetPlayers()) do
	if player.CharacterAdded:Wait() then
		print(player.Name)
	end
end

but still no printing

2 Likes
--< Variables >--
local Players = game["Players"]

--< Functions >--
function PlayerAdded(user)
     user.CharacterAdded:Connect(function()
         print("Character added for" .. user.Name .. "!")
     end
end

--< Functions >--
Players.PlayerAdded:Connect(PlayerAdded)
2 Likes

Like I said, It is looking through the list only once; and when it looks through, there are no players… As most serverscripts are ran before the client is even in.

2 Likes

is there a way to loop through all the players? I’m working on something.

2 Likes

Yes, Your code should work if it’s running at a time when players are already in the server.
However, in this code, there aren’t players in the server because its running before any join.

2 Likes
task.wait(3)
local players = game:GetService("Players")
for _, player in pairs(players:GetPlayers()) do
	if player.Character then
		print(player.Name)
	end
end
2 Likes

I am like 100% sure you dont need the “player.character”, just player suffices.

2 Likes

Just looking to stick with his script as close as possible. You’re probably right.
I was actually looking at that for a bit … thinking why.

Almost posted:

player.Character or player.CharacterAdded:Wait() 

Thinking it might be there for a reason I’m not aware of.

1 Like

This should work.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	for i, player in players:GetChildren()do
		if player then
			print(player.Name)
		end
	end
end)
2 Likes

The for should work, but it should also print 0 because no players have even joined yet, it runs too quickly.

In this example, it checks all players when someone says “/getPlayers”

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if message == "/getPlayers" then
            print(tostring(#game.Players:GetPlayers()).." Players")
            print("- - -")
            for index,player in pairs(game.Players:GetPlayers()) do
                print(tostring(index)..": "..tostring(player.Name))
            end
        end
    end)
end)
1 Like

well, you could just add a localscript in starterplayerscripts and run this:

print(game.Players.LocalPlayer.Name)

if you want to keep server side for something else

function printP(player: Player)
    if player == nil then warn("Player not provided!") return end
    if not player:IsA("Player") then warn("Not a player!") return end
    print(player.Character and player.Name or "Character hasn't loaded.")
end

for _, v in game.Players:GetPlayers() do
    printP(v)
end

game.Players.PlayerAdded:Connect(printP)
1 Like

The others are right the script runs before the players, so what you can do is;

local players = game:GetService("Players")

function loopPlayers()
	for i, v in pairs(players:GetPlayers()) do
		if v.Character then
			print(v.Name)
		end
	end
end

players.PlayerAdded:Connect(loopPlayers)

this one loops the players every time someone joins.

You don’t actually need to run the loop inside the function.

this is probably because the script ran before there are any players in the server.

-- wait for any players.
game.Players.PlayerAdded:Wait()
for i, player in pairs(game.Players:GetPlayers()) do
	if player.Character then
		print(player.Name)
	end
end
1 Like

i put it there maybe s/he may add a .PlayerRemoved one as well

1 Like

This doesn’t guarantee the player will be in the game, and isn’t a way to go about this in the first place.

1 Like