Is it possible to access this dictonary?

So I am starting to use Datastore 2 and I want to try to save every color (or texture) for a car so I tried creating this dictonary but it looks like I cannot go into Cars, then add a mustang for example and give it some sub informations.

local function SetDataTable()
	local UserData = {
		Stats = {
			["Money"] = 0,
			["Chips"] = 0,
		},
		Weapons = {
			["M4"] = false,
			["Pistol"] = false,
		},
		Cars = {
			Mustang = {
				[1] = "Color",
				[2] = "Miau",
				},
		},
		Houses = {
			["House1"] = false,
			["House2"] = false,
		},
	}
	return UserData
end

So I am trying to give mustang some extra infos that I can access.
Is that possible somehow?

2 Likes

you can do:

UserData[1][1] 

which would theoretically retrieve “Money”

UserData.Cars.Mustang[3] = "Hello"

1 Like

@VegetationBush (Mentioning because replying results in an ISE, which is currently being investigated)

The indice of 1 in the UserData table is nil and the second lookup will throw an exception for attempting to perform a lookup on a nil indice. Numerical index lookups only work when you are working with an array or you explicitly use numerical indices.

ClockworkSquirrel’s lookups are the proper way to do this. That is, you can access the table either through chained lookups (the proper way) or dot syntax (syntactic sugar). Valid examples of this in practice as code blocks:

local mustangData = UserData["Cars"]["Mustang"]
local mustangData = UserData.Cars.Mustang
2 Likes

The methode from @ClockworkSquirrel did work when I use it in the same script. However how would I access it from outside via datastore2 ?

normally I would use this to get the values

part.ClickDetector.MouseClick:Connect(function(plr)
	local Cars = DataStore2("Cars", plr):Get()
	print(Cars.Mustang)
end)

but this doesnt work

part.ClickDetector.MouseClick:Connect(function(plr)
	local Cars = DataStore2("Cars", plr):Get()
	print(Cars.Mustang[1])
end)

That depends on your implementation. I recommend you consult documentation and try debugging this code for yourself to understand why it’s not working first, as this is probably an issue you’ll be able to resolve by picking away at it a bit.

Saying that something doesn’t work without providing context makes it difficult for me to respond properly as well. There’s always a reason why something doesn’t work and you should always look into it and provide information rather than just saying it doesn’t work.

1 Like