To Detect players joining and leaving in a table, Would this work?

Hello! I am currently trying to study the Tables in lua!

Would this work to detect players joining?

local MyAwesomeTable = {"PlaceHolder1"}

game.Players.PlayerAdded:Connect(function(player)
	table.insert(MyAwesomeTable[player])
end)


while true do
	print(MyAwesomeTable)
end

When using table.insert, the first argument is the table, 2nd argument the index of the table where you want to place the item (usually it’s set by default to #list + 1, so you can leave it blank if you want), and the 3rd argument is the item you want to insert in the list.

local testTable = {}
table.insert(testTable, "hello") --I left the 2nd parameter blank, so it will be set to default

Also, it’s best to keep a task.wait() in your while loop so that studio won’t freeze.

1 Like

Hello! Thank you for responding!

What is task.wait()? I have seen it and I don’t see a difference from using wait, Can you elaborate me on that please?

Also, it works now thanks!

You’ll want to see the task.wait section of this post:

1 Like

Thanks for the help! What other components do you think I should learn for scripting? : O

Not really sure myself as I’m learning as well, probably CFrames & coroutines

1 Like

Other way around, What other components should I learn for Tables?

local MyAwesomeTable = {"PlaceHolder1"}

game.Players.PlayerAdded:Connect(function(player)
	table.insert(MyAwesomeTable, player)
end)


for i, v in pairs(MyAwesomeTable) do
	print(v)
end

Learn iterator functions, they come hand in hand with tables.

2 Likes

You’ll want to learn other functions for tables such as table.remove, table.create, etc. and also what @Limited_Unique said.

1 Like

As the above post suggests, familiarise yourself with the other functions belonging to the table library too.

table.concat
table.find
table.remove
table.insert
etc.

1 Like

Avoid using while wait() do loops as they are very deoptimized. Replace wait() with task.wait() for a performant code.

1 Like

Why do you think this line of code wont work :thinking:

game.Players.PlayerAdded:Connect(function(player)
	table.insert(MyAwesomeTable, player)
	TextLabel.Text = TextLabel.Text + ", " tostring(player)
end)

Im trying to make it so that the players name will be listed on a 3D board and if more than one player, will list a “,” and then add another players name

Use “..” to concatenate string values in Lua. “+” is the syntax for Python.

1 Like

Seems like it works now, do I need to fix anything in this code to make it smaller and or fix it?

local MyAwesomeTable = {}

local TextLabel = script.Parent

game.Players.PlayerAdded:Connect(function(player)
	table.insert(MyAwesomeTable, player)
	TextLabel.Text = TextLabel.Text..", "..tostring(player)
end)


while true do
	task.wait()
	print(MyAwesomeTable)
end
local MyAwesomeTable = {}
local TextLabel = script.Parent

game.Players.PlayerAdded:Connect(function(player)
	table.insert(MyAwesomeTable, player)
	TextLabel.Text = TextLabel.Text..", "..tostring(player)
end)

while task.wait() do
	print(MyAwesomeTable)
end

Just a slight change but basically the same as yours, bare in mind when you print a table directly like that you’ll get something like this appear in console:

image

If you want to print the items inside of a table you need to traverse over the elements of the table, that can be done like how I did in my first reply to this thread.

1 Like

Here is the place that this is in:

Just asking, why is it that it prints all items in my array in dictionary form when I did something like this? Or does that only happen in output?

Might be a console/output difference.

1 Like