I need to datastore 18 bool values in either a table or dictionary then retrieve them.
My issue is that It looks like it saves the values however it only retrieves one value.
Ive tried looking through how to datastore dictionaries and or tables and I havent been able to figure it out. If im not using a good method then let me know a better way to go about it?
This is the Server Script:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("Thedatastore12345")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local PotionsTable = {
P1 = false,
P2 = false,
P3 = false,
P4 = false,
P5 = false,
P6 = false,
P7 = false,
P8 = false,
P9 = false,
P10 = false,
P11 = false,
P12 = false,
P13 = false,
P14 = false,
P15 = false,
P16 = false,
P17 = false,
P18 = false,
}
local function PlayerAdded(Player)
local DataFromStore = nil
local potfolder = Instance.new("Folder")
potfolder.Name = "PotionsFolder"
potfolder.Parent = Player
for name, value in pairs(PotionsTable) do
local new = Instance.new("BoolValue")
new.Name = name
new.Value = value
new.Parent = potfolder
end
local s, e = pcall(function()
DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
end)
if s then
print("Getting Data For: " .. Player.Name)
elseif e then
warn("Error Getting Data For: " .. Player.Name)
end
if DataFromStore then
local DataMan = game:GetService("HttpService"):JSONDecode(DataFromStore)
print(DataMan)
for name, value in pairs {DataMan} do
Player.PotionsFolder["P" .. name].Value = value
end
print("Data Loaded For: " .. Player.Name)
else
print("Created New Data For: " .. Player.Name)
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for i,v in next,game.Players:GetPlayers() do
PlayerAdded(v)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local DataForStore = {}
for name, value in pairs(Player.PotionsFolder:GetChildren()) do
table.insert(DataForStore, value)
DataForStore[value.Name] = value.Value
print(unpack(DataForStore))
end
TestDataStore:SetAsync("uid-" .. Player.UserId, game:GetService("HttpService"):JSONEncode(DataForStore))
end)
Thanks for any help!