Script just stops without any error message in the output

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.

You haven’t defined playerDataStore.

I have. This is not the complete script. Sorry if that wasn’t clear. I wrote it on the bottom.

Could you show us where you defined it?

Also, I’m not sure what type magic is, but I recommend setting Level and CurrentXP to 0 if no data is found instead of nil.

I have edited the post and included my whole script. Magic is a IntValue. I will probably do something like: 1 = fire 2 = water or something. ID based.

Is there an error/warn in the output?

No errors are displayed. Sometimes it gets through and prints the “GOT HERE” message I put to confirm it got to the end. Other than that, no errors.

Try removing the pcalls and see if there are any errors.

The data still does not save. There are no error messages.

Show me your new script. (30Characters)

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.."DATUM")
	end)
	
	if success then
		print('Data Loaded')
		local dataTable = playerDataStore:GetAsync(key.."DATUM")
		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 = 0,
			CurrentXP = 0,
			Magic = 0
			}
	}
	
	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 function saveThis()
		playerDataStore:SetAsync(key.."DATA", ALLDATA)
	end
	saveThis()
	--if success then print ("SAVED") else print(errormessage) end
print("GOT HERE")
end

return playerData

You missed some pcalls. (30characters)

Oh you wanted me to remove those too okay.

It still did not work. I’m pretty sure the saving is taking too long and it just stop mid-way. Is it possible?

Are there any errors in the output with the pcalls removed?

1 Like

There are no errors in the output.

You’re using 2 different keys, DATA and DATUM, change them both to DATA

How are you calling playerData.SavePlayerData(player)? Do you just use a PlayerRemoving event?

It still did not work even after changing.

Yes, I call the function using PlayerRemoving.