Need help with tables

I’m trying to find the user name of a player in a table using table.find but it returns nil, when I print what is in the table it comes out as this:
Screenshot 2022-08-27 160704
Is there a way that I can make it so it will actually put the name of the player in the table and not a jumble of letters and numbers?

2 Likes

There is no way to do that as far as I know, but you can try to Encode it.

local tbl = {"hi"}
print(game:GetService("HTTPService"):JSONEncode(tbl))

or you could check the output in roblox studio and it’ll show.

First, you have to get all the players in game and place them in an empty table

local players = game.Players
local playerTable = {} --This is an empty table

for i, plr in pairs(players:GetPlayers()) do --Loops throughr every player in game.
   table.insert(plr, playerTable)
end

table.insert puts objects into tables. The first parameter is the object you want to put in a table, and the second parameter is the table you want the object to go to. (If this does nt work, then i must have switched the parameteres. If this doesnt work then the first parameter is the table and the second one is the object.)

Now the table is filled with every player in game, and you can find whatever information you need to find.

That is what I am doing but it is returning a nil value
script:

local MessagingService = game:GetService("MessagingService")
local PlayersInQueue = {}
local TeleportService = game:GetService("TeleportService")
MessagingService:SubscribeAsync("Queue", function(message)
	if string.find(tostring(message.Data), "Add") then
		print(message.Data)
		local NewString = string.sub(message.Data, 1, string.len(message.Data)-3)
		table.insert(PlayersInQueue, NewString)
		print(NewString)
		print(string.sub(message.Data, 1, string.len(message.Data)-3))
	elseif string.find(tostring(message.Data), "Remove") then
		table.remove(PlayersInQueue, table.find(PlayersInQueue, string.sub(message.Data, 1, string.len(message.Data)-3)))
	end	
end)

function FindPlayers(PlayerName)
	print("Lookking")
	local Place = TeleportService:ReserveServer(10738259616)
	wait(1)
	print(PlayersInQueue)
	local Player = table.find(PlayersInQueue, PlayerName)
	print(Player) -- is were it print nil
	local Queued = 1
	TeleportService:TeleportToPrivateServer(10738259616, Place,Player)
	repeat
		local PlayerFind = math.random(1, #PlayersInQueue)
		table.find(PlayersInQueue, PlayerFind)
		Queued = Queued + 1
		TeleportService:TeleportToPrivateServer(10738259616, Place,PlayerFind)
	until Queued == 20
end

game.ReplicatedStorage.Events.EditQueue.OnServerEvent:Connect(function(PlayerName, Join, UserID)
	if Join == true then
		MessagingService:PublishAsync("Queue", tostring(PlayerName).."Add")
		FindPlayers(PlayerName)
		print("Tw")
	else
		MessagingService:PublishAsync("Queue", tostring(PlayerName).."Remove")
	end
end)



1 Like

printing tables itself wont work, youd have to loop through the table and print each value, like so

for i, plr in pairs(playertable) do
   print(plr)
end

but if you want to get a specific persons username you can always try to do this

for i, plr in pairs(playertable) do
   print(plr)
   local username = plr.Username
   if username == "Pyromxnia" then
     --do stuff
   end
end

So, this wont work for getting the players name out of the table?

table.find(PlayersInQueue, PlayerName)

no. I just read the API doccumentation and i think i found the issue.

You have to print that entire line of code.

print(table.find(PlayersInQueue, PlayerName))

You could do:

print(table.concat(playerList, ", "))