How can I find a player's name in a table?

Hey there. So coming back from my break for a few hours to code a quick script on a testing server, just so I can see how I can make a random duo system.

So the way I was going to do this was to make a table titled ‘playerList’, and add players to it when they join. I add the players into the table by there names, and in the order of the number of players:

table.insert(playerList, #game.Players:GetPlayers(), player.Name)

So after that I want to use math.random(1,#game.Players:GetPlayers()) to see which teammate the player will get. After that I want to get the player’s name through the table, but I can’t figure out how I can do that. What can I put in this string.find() as the arguments to see what the player’s spot in the table is, and what their name’s is? Thanks! (Let me know if you don’t understand.)

Full script:

local playerList = {}

function playerAdd(player)
	if not string.find(player.Name, playerList) then
		table.insert(playerList, #game.Players:GetPlayers(), player.Name)
	end
	if #game.Players == 10 then
		local possibleTeamMates = math.random(1,#game.Players:GetPlayers())
		if possibleTeamMates == string.find()
	end
end
game.Players.PlayerAdded:Connect(playerAdd)
local PS = game:GetService('Players')

local RandomPlayer = PS:GetPlayers()[math.random(#PS:GetPlayers())]

print(RandomPlayer.Name)

Hey, but I was wanting to get it from the table. Can I do this possibly?

print(playerList[math.random(#playerList)].Name)

Thanks! I’ll test this in a minute. It’s really appreciated! :slight_smile:

Hang on, it looks to me like there’s an issue there.
Also try:

print(playerList[math.random(#playerList)].Name)

Yeah, I noticed that too. :slight_smile:

Did it work? If so, make sure to press the :white_check_mark:. (:

I modified it a bit just to this:

local playerList = {}

function playerAdd(player)
	table.insert(playerList, #game.Players:GetPlayers(), player.Name)
	while true do
		wait(.25)
		if #game.Players:GetPlayers() == 2 then
			local possibleTeamMates = math.random(1,#game.Players:GetPlayers())
			player.leaderstats.Teammate.Value = playerList[math.random(possibleTeamMates)].Name
		end
	end
	wait(.25)
end
game.Players.PlayerAdded:Connect(playerAdd)

And I’m getting errors like, " 16:19:28.757 - ServerScriptService.wip:9: bad argument #3 to ‘Value’ (string expected, got nil)
16:19:28.758 - Stack Begin
16:19:28.758 - script ‘ServerScriptService.wip’, Line 9
[16:19:28.758 - Stack End]"

I believe your table.insert function is structured incorrectly. Because table.insert appends the value to the end of the current table, you should try:

playerList = game:GetService("Players"):GetPlayers();

that way the .Name value for Line 9 will not be nil and it will return a string.

Hello. I still get my errors? Thanks! :slight_smile:

playerList = game:GetService("Players"):GetPlayers();

function playerAdd(player)
   table.insert(playerList, #game.Players:GetPlayers(), player.Name)
   while true do
   	wait(.25)
   	if #game.Players:GetPlayers() == 2 then
   		local possibleTeamMates = math.random(1,#game.Players:GetPlayers())
   		player.leaderstats.Teammate.Value = playerList[math.random(possibleTeamMates)].Name
   	end
   end
   wait(.25)
end
game.Players.PlayerAdded:Connect(playerAdd)

Oh, I’m sorry. I meant you should remove the table.insert entirely.

Try this:

local Players = game:GetService("Players");
local playerList = Players:GetPlayers();

function playerAdd(player)
   while true do
   	wait(0.25);
   	if #Players:GetPlayers() == 2 then
   		local possibleTeamMates = math.random(1,#Players:GetPlayers())
   		player.leaderstats.Teammate.Value = playerList[math.random(possibleTeamMates)].Name
   	end
   end
   wait(.25)
end
game.Players.PlayerAdded:Connect(playerAdd)

Oh alright, thanks! Just at a rodeo right now, will test it in a few hours. :slight_smile:

1 Like

Disaster strikes when the playerList is empty or 1. Incredible!!!

local Players = game:GetService("Players");

local function onPlayerAdd(player)
    while player.Parent do -- loop fixed
        wait(0.25);
   	    if #Players:GetPlayers() == 2 then
   		    local possibleTeamMates = math.random(1,#Players:GetPlayers())
   		    player.leaderstats.Teammate.Value = Players:GetPlayers()[math.random(possibleTeamMates)].Name
   	    end
    end
    wait(.25)
end

Players.PlayerAdded:Connect(playerAdd)

I believe he wanted the playerList for comparison and other functions or he wouldn’t have it listed. But yes, if he didn’t, that would be a better solution.

Why are you doing:

local possibleTeamMates = math.random(1,#Players:GetPlayers())

and then doing

Players:GetPlayers()[math.random(possibleTeamMates)].Name

Shouldn’t it be:

Players:GetChildren()[math.random(1, #Players:GetChildren())].Name

You can’t just throw a non number value into math.random that is obviously gonna send you an error.

You could also try a for loop that searches through the array for the name.