Hello, this is my first post! I want to save the IntValues saved in the Inventory folder. However, this script just stops in the middle of the code, right before the :SetAsync. Sometimes it gets through and prints the final message I put, sometimes the output shows nothing and nothing happens. I believe this is because the player has already left and the data only saves sometimes because we got the timing right/the player left slower. If so, is there a possible fix? Here’s the code:
local playerData = {}
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDATA")
local AllPlayerData = game:GetService("ReplicatedStorage"):WaitForChild("AllPlayerData")
function playerData.LoadPlayerData(player)
local key = player.UserId
local success, errormessage = pcall(function()
playerDataStore:GetAsync(key.."DATA")
end)
if success then
print('Data Loaded')
local dataTable = playerDataStore:GetAsync(key.."DATA")
if dataTable ~= nil then
print("here")
local playerFolder = Instance.new("Folder", AllPlayerData)
playerFolder.Name = player.Name
local inventoryFolder = Instance.new("Folder", playerFolder)
inventoryFolder.Name = "Inventory"
local statsFolder = Instance.new("Folder", playerFolder)
statsFolder.Name = "Stats"
local ArmorFolder = Instance.new("Folder", playerFolder.Inventory)
ArmorFolder.Name = "Armor"
local WandFolder = Instance.new("Folder", playerFolder.Inventory)
WandFolder.Name = "Wand"
local MiscFolder = Instance.new("Folder", playerFolder.Inventory)
MiscFolder.Name = "Misc"
local EquippedWandFolder = Instance.new("Folder", playerFolder.Inventory)
EquippedWandFolder.Name = "EquippedWand"
local EquippedArmorFolder = Instance.new("Folder", playerFolder.Inventory)
EquippedArmorFolder.Name = "EquippedArmor"
for i, v in pairs(dataTable.Inventory.Armor) do
local newItem = Instance.new("IntValue")
newItem.Parent = playerFolder.Inventory.Armor
newItem.Name = v
end
for i, v in pairs(dataTable.Inventory.Wand) do
local newItem = Instance.new("IntValue")
newItem.Parent = playerFolder.Inventory.Wand
newItem.Name = v
end
for i, v in pairs(dataTable.Inventory.Misc) do
local newItem = Instance.new("IntValue")
newItem.Parent = playerFolder.Inventory.Misc
newItem.Name = v
end
for i, v in pairs(dataTable.Inventory.EquippedWand) do
local newItem = Instance.new("IntValue")
newItem.Parent = playerFolder.Inventory.EquippedWand
newItem.Name = v
end
for i, v in pairs(dataTable.Inventory.EquippedArmor) do
local newItem = Instance.new("IntValue")
newItem.Parent = playerFolder.Inventory.EquippedArmor
newItem.Name = v
end
local statsLevelOBJ = Instance.new("IntValue")
statsLevelOBJ.Parent = playerFolder.Stats
statsLevelOBJ.Value = dataTable.Stats.Level
statsLevelOBJ.Name = "Level"
local currentXPOBJ = Instance.new("IntValue")
currentXPOBJ.Parent = playerFolder.Stats
currentXPOBJ.Value = dataTable.Stats.CurrentXP
currentXPOBJ.Name = "CurrentXP"
local magicOBJ = Instance.new("IntValue")
magicOBJ.Parent = playerFolder.Stats
magicOBJ.Value = dataTable.Stats.Magic
magicOBJ.Name = "Magic"
else
print("GOT TO ELSE")
--//NEW DATA CREATION\\--
local playerFolder = Instance.new("Folder", AllPlayerData)
playerFolder.Name = player.Name
local INVENTORYFOLDER = Instance.new("Folder",playerFolder)
INVENTORYFOLDER.Name = "Inventory"
local ARMORFOLDER = Instance.new("Folder", INVENTORYFOLDER)
ARMORFOLDER.Name = "Armor"
local MISCFOLDER = Instance.new("Folder", INVENTORYFOLDER)
MISCFOLDER.Name = "Misc"
local WANDFOLDER = Instance.new("Folder", INVENTORYFOLDER)
WANDFOLDER.Name = "Wand"
local EQUIPPEDWAND = Instance.new("Folder", INVENTORYFOLDER)
EQUIPPEDWAND.Name = "EquippedWand"
local EQUIPPEDARMOR = Instance.new("Folder", INVENTORYFOLDER)
EQUIPPEDARMOR.Name = "EquippedArmor"
local STATSFOLDER = Instance.new("Folder", playerFolder)
STATSFOLDER.Name = "Stats"
local LEVEL = Instance.new("IntValue", STATSFOLDER)
LEVEL.Name = "Level"
LEVEL.Value = 1
local MAGIC = Instance.new("IntValue", STATSFOLDER)
MAGIC.Name = "Magic"
MAGIC.Value = 0
local CURRENTXP = Instance.new("IntValue", STATSFOLDER)
CURRENTXP.Name = "CurrentXP"
CURRENTXP.Value = 0
end
else
print(errormessage)
end
end
function playerData.SavePlayerData(player)
print("YES")
local key = player.UserId
local DATATOSAVE = game:GetService("ReplicatedStorage"):WaitForChild("AllPlayerData"):WaitForChild(player.Name)
local ALLDATA = {
Inventory = {
Armor = {},
Wand = {},
Misc = {},
EquippedArmor = {},
EquippedWand = {}
},
Stats = {
Level = nil,
CurrentXP = nil,
Magic = nil
}
}
for i, v in pairs (DATATOSAVE.Inventory.Armor:GetChildren()) do
ALLDATA.Inventory.Armor[#ALLDATA.Inventory.Armor+1] = v.Name
end
for i, v in pairs (DATATOSAVE.Inventory.Wand:GetChildren()) do
ALLDATA.Inventory.Wand[#ALLDATA.Inventory.Wand+1] = v.Name
end
for i, v in pairs (DATATOSAVE.Inventory.Misc:GetChildren()) do
ALLDATA.Inventory.Misc[#ALLDATA.Inventory.Misc+1] = v.Name
end
for i, v in pairs (DATATOSAVE.Inventory.EquippedArmor:GetChildren()) do
ALLDATA.Inventory.EquippedArmor[#ALLDATA.Inventory.EquippedArmor+1] = v.Name
end
for i, v in pairs (DATATOSAVE.Inventory.EquippedWand:GetChildren()) do
ALLDATA.Inventory.EquippedWand[#ALLDATA.Inventory.EquippedWand+1] = v.Name
end
ALLDATA.Stats["Level"] = DATATOSAVE.Stats.Level.Value
ALLDATA.Stats["CurrentXP"] = DATATOSAVE.Stats.CurrentXP.Value
ALLDATA.Stats["Magic"] = DATATOSAVE.Stats.Magic.Value
local success, errormessage = pcall(function()
playerDataStore:SetAsync(key.."DATA", ALLDATA)
end)
if success then print ("SAVED") else print(errormessage) end
print("GOT HERE")
end
return playerData
I have another script calling the function when the player leaves.