In pairs only gets 1 player

  1. What do you want to achieve? I want to achieve a script that loops thru all the players.

  2. What is the issue? My In pairs function doesn’t get all the players. It only gets 1 player (yes i have tested with other people)

  3. What solutions have you tried so far? Changing it to Ipairs, pairs, Making "Game.Players:GetChildren(), Making Game.Players:GetPlayers()

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!

function CheckSight(Players)
	-- THE "PLAYERS" Variable IS game.Players:GetPlayers()
	
	for i, v in ipairs(Players) do
		local char = v.Character
		print(v)
      end
end

Heres a preview of the script. It only prints 1 player name.

1 Like

if the Players Variable is saved at the beginning of the script (the start of the server). It wont update when a player joins/leaves (since its a preassigned table)

function CheckSight(Players)
	-- Remove the "Players" Variable
	
	for i, v in ipairs(game.Players:GetPlayers()) do
		local char = v.Character
		print(v)
	end
end

It is made right before the function is called

local Players = game.Players:GetPlayers()
local Result = CheckSight(Players)

I also have tried it like that. Thats how it looked in the start.

Still really not a good option, if a player joins when the function is called, he wont be included

Use pairs

function CheckSight(Players)
	-- Remove the "Players" Variable
	
	for _, v in pairs(game.Players:GetPlayers()) do
		local char = v.Character
		print(v)
	end
end

Have tried that too doesnt change anything

You must need print character?

Try again:

function CheckSight(Players)
	-- Remove the "Players" Variable
	
	for _, v in pairs(game.Players:GetPlayers()) do
		local char = v:WaitForChild("Character")
		print(v)
	end
end

That won’t make a difference. pairs and ipairs are basically identical, just that ipairs is better for looping through arrays.

That isnt the problem. The problem is that it doesn’t get all players. Print(v) doesn’t have anything with the char todo. It shall print the Instances name witch is the Player.

I thought that was maybe solution

Try printing the length of the array, to see if the issue has something to do with your loop.

print(#Players)
for _, v in pairs(game.Players:GetPlayers())
  print(v.Character)
wait()
end

No. This is not how it works. Please research before trying to answer questions with false information.

Alright ill test that also:

Run.Heartbeat:Connect(function()
	Playerss = game.Players:GetPlayers()
end)

I have tried doing that to test if it was because it only checked when the script that made.
But this doesn’t change a thing.

This won’t change anything, either. Try doing what I said.

It prints 2 Constantly. When im testing with 2 players

But it only prints 1 player name in the Print player function.

Yeah, alright. The issue should be you doing player:WaitForChild("Character"), since the Character is a property of the player, not a instance. Change it to player.Character.