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

How would I convert a dictionary table to a standard array?

I'm trying to save some values to a datastore, however you can't save dictionaries. I have one below as an example:

{ ["Money"] = 0, ["Cars"] = "One, two, three", ["DistanceWalked"] = 16 }

EDIT: This is the code I'm using - The SaveOther folder contains about 6 values, the name of all of them are strings, but the values themselves are integers AND strings.

local datastore = game:GetService("DataStoreService")
while true do
wait (30)
print ("lets go")
local players = game.Players:GetChildren()
for i=1, #players do
local player = players[i]
local cardata = player.PlayerData
local playerstats = datastore:GetDataStore("TempStats")
--cardata = playerstats:SetAsync(player.UserId.."_Stats",cardata)
local values = cardata.SaveOther:GetChildren()
local mytable = {}
for a=1, #values do
mytable[values[a]] = values[a].Value
print (mytable[values[a]])
end
print(mytable)
local testtable = {}
testtable[1] = mytable
print(tostring(player.UserId.."_Stats2"))
local key = tostring(player.UserId.."_Stats2")
playerstats:SetAsync(key, testtable)
end
end

I get the "09:50:24.466 - keys must be strings" error, although I can't understand why they're not strings.

3 comments

0

Who says you can't save dictionaries into a datastore? As long as EITHER all the keys are strings OR all the keys are sequential ints from 1 to x, you should be able to save it into a datastore. http://wiki.roblox.com/index.php?title=Data_store#Tables_with_mixed_list_.2F_record_style_will_be_cut_off

1

I'll update teh question with my code and the error I get, someone had said on a forum post that the values have to be arrays instead of dictionaries.

0

Unrelated, but do :GetPlayers() instead of :GetChildren() for line 5.

1 answer

✓ Accepted answer

values is a table of objects, returned by GetChildren() from line 11.

When you do mytable[values[a]] on line 14, your setting the keys of mytable to an object. As you may already know, the keys of tables uploaded to a datastore cannot be objects.

I think you meant to do mytable[a] on line 14. This should fix your problem.

1 comment

1

Thanks! I actually meant mytable[values[a].Name], I had spent so long on such a basic mistake.

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