How do I make this script print all the players in the table?

I’m starting to confuse my self while writing this, just need another eye.

for i,v in pairs(PlayerTable)do
			
			local Player = game.Players[v.Name]

			RemoteEvent:FireClient(Player)
			print(Player)
end
1 Like

Assuming that PlayerTable == game.Players:GetPlayers()

for i, v in pairs(PlayerTable) do
 print(v.Name)
end
1 Like

so I’m running a script that puts players in the table if they touch a Part.

and in a different function, I’m trying to retrieve only the players in that table.

Have you checked your table to ensure that it only has valid unique player objects?

Or are you needing to skip nonplayers?

Ummm… idk? Here is my touch script

local Playercounter = script.Parent

Playercounter.Touched:Connect(function(Object)
	if Object.Parent:FindFirstChild("Humanoid") then
		if Object.Name == "Torso" or Object.Name == "TorsoUpp"then	
			local Player = game.Players[Object.Parent.Name]	
			table.insert(PlayerTable,Player)
		end
	end
end)

To store all players that touched a specific part, I’d store it in a separate table and use the :GetPlayerFromCharacter() method of the Players Service to check if the character that touched the part is a player.

-- Assuming this script is parented to a part object

local Players = game:GetService("Players")

local playerPartCache = {} -- table of all players that touched the part

local part = script.Parent

part.Touched:Connect(function(hit)
	local Character = hit.Parent -- check for character
	local Player = Players:GetPlayerFromCharacter(Character) -- check for player
	
	if Character and Player and not playerPartCache[Player.Name] then -- "not playerPartCache[player.Name]" means if the player is not in the table already.
		playerPartCache[Player.Name] = Player -- Inserts player into table
	end
end)
1 Like

Does this store the player’s name or the Player its self

It stores the player’s Name as the key and the player’s object as the value.

so in a different function, I can do

while Wait(1) do
   for key, value in pairs do
      print(key,value)
   end
end

and get all the players Names as then I and characters as the V?

yep, that’s assuming you setup the table that way such as “playerPartCache[player.Name] = player.Character”

would this work for all players?

local OtherPart = workspace......

while Wait(1) do
   for key, value in pairs do
      Value.Touched:Connect(Function(hit)
         if hit.Name == "OtherPart" then
         table.remove(playerPartCache,value)
      end)
   end
end

What exactly are you trying to do there?

so when the a player in the table touches “OtherPart” im trying to then remove them from the table. This works but only for one player

It can do that for every player. Just make sure you do the proper checks.

Thank you! :slight_smile: smile for you :slight_smile:

1 Like