Reading a Variable that's in a dictionary

I’m currently trying to have a system that saves a lot of things, without using a lot of datastores because I think it looks tacky, and so I’m saving a dictionary.

For some reason though, I can’t actually find how to get the value from the variables in said dictionary, so I’m wondering how I would do this?

I’m guessing it’s something dumb that I really should have thought of.

1 Like

These variables are what’s called “fields”. If the field’s key is a string, you can access it either with dict.key or dict["key"].

local dict = {a = 5}

print(dict["a"], dict.a)

If the key is any other value, you are forced to use the [] way. Same applies for defining the fields inside of the dictionary, you can either do {a = 5} or {["a"] = 5}, with again the constraint of only being able to use [] for any value other than strings.

Let’s say your dictionary looks like this:

local dictionary = {
Variable == 5
}

You would do

dictionary["Variable"]

to get the variable.

You could just reference it as like you’re attempting to find the Ancestor of a certain object after your pcall you want to obtain your Data

--Saving this in a PlayerRemoving event
local ThingsToSave = {
    Kills = player.leaderstats.Kills.Value;
    Gold = player.leaderstats.Gold.Value;
}

--Doing our pcall function to get the data in our PlayerAdded event
if Data and success then
    KillsStat.Value = ThingsToSave.Kills
    GoldStat.Value = ThingsToSave.Gold
end

yeah I can get the variable but it never gives me the value of that variable, also I know it’s an error with the loading method, as I’ve done a bunch of debugging to find where it fails

Showing the script could help us figure out your issue :thinking:

My script is :

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("Cosmetics")

local CosmeticsNameList = {placeholder = false, placeholder2 = false}

game.Players.PlayerAdded:Connect(function(player)
	
	local cosmetics = Instance.new("Folder")
	cosmetics.Name = "Cosmetics"
	cosmetics.Parent = player

	local playerUserId = "Player_"..player.UserId
	local data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			for i, v in pairs(data) do
				local bool = Instance.new("BoolValue", cosmetics)
				bool.Name = i
				bool.Value = data
				print(bool.Value)
			end
		else
			print("Failed")
		end
	else 
		print("Error Loading Data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local playerUserId = "Player_"..player.UserId

	local data = CosmeticsNameList
	
	for i, v in pairs(player.Cosmetics:GetChildren()) do
		data[v.Name] = v.Value
	end

	local success, errormessage = pcall(function()
		print("sync done")
	end)

	if success then
		print("Data Saved!")
		myDataStore:SetAsync(playerUserId,data)
	else 
		print("Error Saving Data")
		warn(errormessage)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		local playerUserId = "Player_"..player.UserId
		
		local data = CosmeticsNameList
		
		for i, v in pairs(player.Cosmetics:GetChildren()) do
			data[v.Name] = v.Value
		end
		
		local success, errormessage = pcall(function()
			print("sync done")
		end)
		
		if success then
			print("Data Saved!")
			myDataStore:SetAsync(playerUserId,data)
		else 
			print("Error Saving Data")
			warn(errormessage)
		end
	end
end)

bool.Value = data should be changed to: bool.Value = v

Yeah I had actually just noticed that lol, and just about to test to see if it loads properly now.

Another note that you can’t index string values if they have spaces.

local t = {["aaaadh dfhdfgjtrsety43 34634"] = 1}
print(t["aaaadh dfhdfgjtrsety43 34634"]) -- valid
1 Like