Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

4 comments

0

Remove the parentheses, and for keep.(Name), it’s keep[name].

0

But why though? What is wrong with nested tables? i.e. playersAnswers[player][Q1A] = Q1A.Value

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)

0

My fault, try the answer below. You can access data this way once it is created..

2 answers

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}

2 comments

0

Don’t make a table hold a table with another table

0

is that bot what OP wanted?

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"

5 comments

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)

0

line 26 ^

0

Seems like nested tables are not supported by DataStores.

0

How should I save these answers then?

0

It would probably be best if you can actually provide line 47, and upto 47.

This discussion is preserved as it was. New answers, comments, and votes are closed.