So Im making a table with an index and a value. How do I put an index in a table?
The index is the identifier of the value i.e identifier = value
. Just like setting the name of a variable, you do the same thing but wrapped inside the curly brackets like
local Table = {
Index = 1
}
print(Table.Index) -- Output: 1
To write a new index, you have to associate a value with that index like so:
Table[Index] = Value
where the square brackets indicate you’re indexing a value from the table and the equal sign indicates you’re setting the value of the index to what’s after the equal sign.
You need brackets around the string and (idk if you meant to but) if you want to make usernameHere a table then you need to say userNameHere = {}
instead of `usernameHere,"
local myTable = {
["usernameHere"] = {
["Question"] = "response",
["Coins"] = 1000
}
}
You can also do:
local myTable = {
usernameHere= {
Question = "response",
Coins= 1000
}
}
Some examples where you need to use brackets is if your word starts with a number or the index isn’t a word but instead something such as a part:
local myTable = {
["0test"] = "hello world",
[workspace.Part] = true
}
Would this work?
local a =
{
{
"usernameHere",
{
["Question"] = "response",
"blah",
"bah blah"
}
}
}
Are you trying to make usernameHere contain a question with a response?
No. Its for an application center, with multiple:
{
"usernameHere",
{
["Question"] = "response",
["Question"] = "response"
}
}
usernameHere is replaced with the person who submitted an application, then the index of [“Question”], is the question, and the value is the response.
So now this is it:
local questions = {
["Donzee_Boi"] =
{
{Question = "This is a question?", Response = "response!!"},
{Question = "This is another question?", Response = "response two!"},
}
}
but how can I use table.insert to add another username and table with questions and responses?
function addQuestion(player, question,answer)
--create table for the player if it doesn't already exist
if not questions[player.Name] then
questions[player.Name] = {}
end
local t = {Question = question, Answer = answer}
table.insert(questions[player.Name], t)
end
game.Players.PlayerAdded:Connect(function(player)
addQuestion(player, "Why did the chicken cross the road?","To get to the other side!")
end)
You don’t. Directly index the table through string keys. table.insert creates a number index and shifts if necessary.
table["Identifier"] = {...}
With tables, you don’t have to define the keys right away.
-- Example code of adding existing players to a table
local questions = {}
for _, Player in ipairs(game:GetService("Players"):GetPlayers()) do
questions[Player.UserId] = {
{
{Question = "This is a question?", Response = ""}
{Question = "This is a question?", Response = ""}
}
}
end
You don’t need to use table.insert
to add keys to a table, as long as the variable is defined to be a table beforehand. Also, I believe table.insert
is restricted to number key values, so if you were planning to use Player.Name
instead of Player.UserId
, the method above would still work but table.insert
wouldn’t.
Tables can be tricky if you don’t know a lot of the ins-and-outs, it’s good to review how they behave by experimenting with them (assuming you have the time for that)
I hope this link will help you
Also, I believe
table.insert
is restricted to number key values
what do you mean by this?
table.insert
won’t accept a string position for example. i.e.,
table.insert({}, 1 2) --> {2}
table.insert({34}, 1, 2) --> {2, 34}
table.insert({}, "non-integer key", 1) --> doesn't work
table.insert
is reserved for arrays, it doesn’t apply to dictionaries (as far as I know). This restricts using a player’s UserId, rather than a name (which honestly, doesn’t really matter because most often people use the UserId anyways). It’s good to know though.