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

Loading datastore in less lines of code?

So I have many variables for players and I load them in like this:

for i = 100, 1, -1 do
	local Item = Instance.new("StringValue", ItemData))
	Item.Name = "Item" ..i

	Item.Value = Data.(ItemPosition ..i)
end

Not sure how to make the line above work, or even if it is possible, here it is again:

Item.Value = Data.(ItemPosition ..i)

The values are loaded above, here's one:

local Data = DataStore:GetAsync(Player.userId) or {}

Data.Item1 = Data.Item1 or 0
Data.Item2 = Data.Item2 or 0
Data.Item3 = Data.Item3 or 0
--etc..

Not sure if you can make that quicker too as well ^

1 answer

✓ Accepted answer

Indexing with . is sugar for indexing using [] and a string.

game.Workspace
-- same as
game["Workspace"]

Instance.new
-- same as
Instance["new"]

Thus if i is 5, Data["Item" .. i] will be the same as Data.Item5.


Usually if you have many "numbered" fields like this, it just means you should be using a list in the first place, though.

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