So, I have a data store that saves a table. It mostly works except that the data it is supposed to save is somehow getting lost when it’s being retrieved. Here is an example: Saved data: [250,"Tiro",0,"Default","0, 0, 0"] | Retrieved Data: [250,"250",null,null]
Here is my code:
--OtherServices
local HttpService = game:GetService("HttpService")
--Data Folders
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SaveInfoFolder = ReplicatedStorage:WaitForChild("SaveInfo")
local CampaignFolder = SaveInfoFolder:WaitForChild("Campaign")
local PlayerDataFolder = CampaignFolder:WaitForChild("PlayerData")
local InventoryFolder = PlayerDataFolder:WaitForChild("Inventory")
local KeyBindingsFolder = SaveInfoFolder:WaitForChild("KeyBindings")
local SettingsFolder = SaveInfoFolder:WaitForChild("Settings")
--DataStore Variables
local DataStoreService = game:GetService("DataStoreService")
local CampaignDataStore = DataStoreService:GetDataStore("CampaignData")
local KeyBindingsDataStore = DataStoreService:GetDataStore("KeyBindingsData")
local SettingsDataStore = DataStoreService:GetDataStore("SettingsData")
local CampaignData = {
Denarii = 0,
XP = 0,
Rank = "Tiro",
Face = "Default",
SkinTone = Color3.new(0, 0, 0)
}
local KeyBindingsData = {
Attack = "LeftMouseButton",
Block = "RightMouseButton",
EquipPrimary = "One",
EquipSecondary = "Two"
}
local SettingsData = {
BodyDecayCooldown = 30,
MaximumUnits = 200,
MainVolume = 0.5,
MusicVolume = 0.5
}
--Code
game.Players.PlayerAdded:Connect(function(Player)
local success, data = pcall(function()
local Data = CampaignDataStore:GetAsync(Player.UserId)
print(Data) --Returns [250,"250",null,null]
DeserializedData1 = HttpService:JSONDecode(Data)
print(DeserializedData1)
end)
local success, data = pcall(function()
local Data = KeyBindingsDataStore:GetAsync(Player.UserId)
DeserializedData2 = HttpService:JSONDecode(Data)
end)
local success, data = pcall(function()
local Data = SettingsDataStore:GetAsync(Player.UserId)
DeserializedData3 = HttpService:JSONDecode(Data)
end)
DeserializedData1 = DeserializedData1 or CampaignData
DeserializedData2 = DeserializedData2 or KeyBindingsData
DeserializedData3 = DeserializedData3 or SettingsData
CampaignData = DeserializedData1
KeyBindingsData = DeserializedData2
SettingsData = DeserializedData3
print("Data Collected")
print(CampaignData)
for i, data in PlayerDataFolder:GetChildren() do
if data.Name == "Denarii" then
print(data)
print(data.Value)
data.Value = CampaignData[1]
elseif data.Name == "XP" then
print(data)
print(data.Value)
data.Value = CampaignData[2]
elseif data.Name == "Rank" then
print(data)
print(data.Value)
print(CampaignData[3])
data.Value = CampaignData[3]
elseif data.Name == "Face" then
print(data)
print(data.Value)
data.Value = CampaignData[4]
elseif data.Name == "SkinTone" then
print(data)
print(data.Value)
data.Value = CampaignData[5]
end
end
for i, data in KeyBindingsFolder:GetChildren() do
if data.Name == "Attack" then
data.Value = KeyBindingsData[1]
elseif data.Name == "Black" then
data.Value = KeyBindingsData[2]
elseif data.Name == "EquipPrimary" then
data.Value = KeyBindingsData[3]
elseif data.Name == "EquipSecondary" then
data.Value = KeyBindingsData[4]
end
end
for i, data in SettingsFolder:GetChildren() do
if data.Name == "BodyDecayCooldown" then
data.Value = SettingsData[1]
elseif data.Name == "MaximumUnits" then
data.Value = SettingsData[2]
elseif data.Name == "MainVolume" then
data.Value = SettingsData[3]
elseif data.Name == "MusicVolume" then
data.Value = SettingsData[4]
end
end
while wait(300) do
CampaignData[1] = tonumber(PlayerDataFolder.Denarii.Value)
CampaignData[2] = PlayerDataFolder.Rank.Value
CampaignData[3] = tonumber(PlayerDataFolder.XP.Value)
CampaignData[4] = tonumber(PlayerDataFolder.SkinTone.Value)
KeyBindingsData[1] = KeyBindingsFolder.Attack.Value
KeyBindingsData[2] = KeyBindingsFolder.Block.Value
KeyBindingsData[3] = KeyBindingsFolder.EquipPrimary.Value
KeyBindingsData[4] = KeyBindingsFolder.EquipSecondary.Value
SettingsData[1] = tonumber(SettingsFolder.BodyDecayCooldown.Value)
SettingsData[2] = tonumber(SettingsFolder.MaximumUnits.Value)
SettingsData[3] = tonumber(SettingsFolder.MainVolume.Value)
SettingsData[4] = tonumber(SettingsFolder.MusicVolume.Value)
local SerializedCampaignData = HttpService:JSONEncode(CampaignData)
local SerializedKeyBindingsData = HttpService:JSONEncode(KeyBindingsData)
local SerializedSettingsData = HttpService:JSONEncode(SettingsData)
local success, data = pcall(function()
CampaignDataStore:SetAsync(Player.UserId, SerializedCampaignData)
end)
local success, data = pcall(function()
KeyBindingsDataStore:SetAsync(Player.UserId, SerializedKeyBindingsData)
end)
local success, data = pcall(function()
SettingsDataStore:SetAsync(Player.UserId, SerializedSettingsData)
end)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
CampaignData[1] = tonumber(PlayerDataFolder.Denarii.Value)
CampaignData[2] = PlayerDataFolder.Rank.Value
CampaignData[3] = tonumber(PlayerDataFolder.XP.Value)
CampaignData[4] = PlayerDataFolder.Face.Value
CampaignData[5] = tostring(PlayerDataFolder.SkinTone.Value)
print(PlayerDataFolder.SkinTone.Value)
KeyBindingsData[1] = KeyBindingsFolder.Attack.Value
KeyBindingsData[2] = KeyBindingsFolder.Block.Value
KeyBindingsData[3] = KeyBindingsFolder.EquipPrimary.Value
KeyBindingsData[4] = KeyBindingsFolder.EquipSecondary.Value
SettingsData[1] = tonumber(SettingsFolder.BodyDecayCooldown.Value)
SettingsData[2] = tonumber(SettingsFolder.MaximumUnits.Value)
SettingsData[3] = tonumber(SettingsFolder.MainVolume.Value)
SettingsData[4] = tonumber(SettingsFolder.MusicVolume.Value)
local SerializedCampaignData = HttpService:JSONEncode(CampaignData)
local SerializedKeyBindingsData = HttpService:JSONEncode(KeyBindingsData)
local SerializedSettingsData = HttpService:JSONEncode(SettingsData)
print(CampaignData)
print(SerializedCampaignData) --Returns [250,"Tiro",0,"Default","0, 0, 0"]
local success, data = pcall(function()
CampaignDataStore:SetAsync(Player.UserId, SerializedCampaignData)
end)
local success, data = pcall(function()
KeyBindingsDataStore:SetAsync(Player.UserId, SerializedKeyBindingsData)
end)
local success, data = pcall(function()
SettingsDataStore:SetAsync(Player.UserId, SerializedSettingsData)
end)
end)
Edit:
View my reply: SetASync() not succeeding or failing? - #2 by MRBOGO_YT