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.
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.
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
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:
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.