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

How do I make a table which is named what the players username is?

Asked by 5 years ago

I am trying to make an application form game. Right now I am to the point where I have answers to questions stored in a folder (workspace.info.answers). I am going to take the answers, put them into a table which is named as their username. I'm just clueless on how to make the table named as their username, then put it into the 'keep' table.

local function onCompleteAppFired(player)
    print("submit pressed")
    local name = player.Name

    local keep = game:GetService('DataStoreService'):GetDataStore('keep'):GetAsync('table') print(keep) --load 'keep'

    local Q1A = workspace.info.answers.Q1A 
    local Q2A = workspace.info.answers.Q2A
    local Q3A = workspace.info.answers.Q3A
    local Q4A = workspace.info.answers.Q4A
    local Q5A = workspace.info.answers.Q5A

    (name) = {  --make a table named as the players username
        "Q1A" = (Q1A.Value),
        "Q2A" = (Q2A.Value),
        "Q3A" = (Q3A.Value),
        "Q4A" = (Q4A.Value),
        "Q5A" = (Q5A.Value)
        }

    keep.(name) = (name) --add the tabel to 'keep'

    DataStoreService:GetDataStore('keep'):SetAsync('table',keep) --save 'keep'
end

This function is running, as I am getting "submit pressed" in the output.

0
Remove the parentheses, and for keep.(Name), it’s keep[name]. Ziffixture 6913 — 5y
0
But why though? What is wrong with nested tables? i.e. playersAnswers[player][Q1A] = Q1A.Value sleazel 1287 — 5y
0
@sleazel , I did 'keep[player.Name][Q1A] = Q1A.Value' for each Q, and I got error: ServerScriptService.remoteevents:25: attempt to index field '?' (a nil value) devisings 58 — 5y
0
My fault, try the answer below. You can access data this way once it is created.. sleazel 1287 — 5y

2 answers

Log in to vote
0
Answered by
farizarps 132
5 years ago

since its being put into a table you can use table indexing to name your variable as a string.

keep[player.Name] = {what ever thetableis}

0
Don’t make a table hold a table with another table Ziffixture 6913 — 5y
0
is that bot what OP wanted? farizarps 132 — 5y
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
5 years ago
Edited 5 years ago

How about this (nested tables):

(You may need to purge old keep table from data strores)

local function onCompleteAppFired(player)
    print("submit pressed")
    local name = player.Name

    local keep = game:GetService('DataStoreService'):GetDataStore('keep'):GetAsync('table') print(keep) --load 'keep' 

    local Q1A = workspace.info.answers.Q1A 
    local Q2A = workspace.info.answers.Q2A
    local Q3A = workspace.info.answers.Q3A
    local Q4A = workspace.info.answers.Q4A
    local Q5A = workspace.info.answers.Q5A

   keep[player.UserId] = {  --use UserId to comply with data protection
        ["Q1A"] = Q1A.Value,
        ["Q2A"] = Q2A.Value,
        ["Q3A"] = Q3A.Value,
        ["Q4A"] = Q4A.Value,
        ["Q5A"] = Q5A.Value
        }


    DataStoreService:GetDataStore('keep'):SetAsync('table',keep) --save 'keep'
end

Edit: Sorry I forgot about brackets.

Edit 2: Ok I have tried to run it, and it actually does not work. I do apologize, while I have been using nested tables a lot, I actually never tried to save them to data strores, which seems is not supported. I get

" 23:59:36.363 - Cannot convert mixed or non-array tables: keys must be strings"

0
I have this now: https://pastebin.com/dquDC0xy. When I try to print I get error: ServerScriptService.remoteevents:47: attempt to index field '?' (a nil value) devisings 58 — 5y
0
line 26 ^ devisings 58 — 5y
0
Seems like nested tables are not supported by DataStores. sleazel 1287 — 5y
0
How should I save these answers then? devisings 58 — 5y
0
It would probably be best if you can actually provide line 47, and upto 47. Ziffixture 6913 — 5y

Answer this question