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.
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.
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
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)