Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I use table.insert when I don't know the index number?

Asked by 3 years ago

I want to create something inside of a table when a function is triggered, I also don't know it's index number. How can I get Roblox to print the newly added thing in the table?

local Players = game.Players

PlayerList = {}

Players.PlayerAdded:Connect(function(player)
    table.insert(PlayerList,1,player.name)
end)

print(PlayerList[1]) --- I don't know the index number.

I mainly want to know how to print the index number, any other mistake is most likely a typo (If not please point it out).

1
If you want the last value in your Table you can do print(PlayerList[#PlayerList]) Is that what you wanted? Spjureeedd 385 — 3y
1
And as for you Players variable, I would suggest you do game:GetService("Players") instead of just game.Players Spjureeedd 385 — 3y

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
local Players = game:GetService("Players")

PlayerList = {}

Players.PlayerAdded:Connect(function(player)
    PlayerList[#PlayerList + 1] = player.Name -- Replacing the Table Indexs Total plus 1 to the player.Name, in that way, table with 0 index plus 1 will be 1, and it replaces the 1st table position to the player.Name.
    print(PlayerList[#PlayerList .." joined the game."]) -- Get the total index in PlayerList, if there is 1 index, it prints the first index.
end)
0
This didn't work, it instead printed 'nil'. nafey200 59 — 3y
0
Put it on ServerScriptService and put it as a script. Xapelize 2658 — 3y
0
I'm pretty sure it worked for me Xapelize 2658 — 3y
Ad

Answer this question