Why is data store not working?

I’m making a script that saves a player’s inventory. It works by creating a folder for the player every time that player joins the game. Then it stores the items that the player owns in to that folder. The folder is essentially the player’s inventory. Unfortunately, this script don’t work.

I’ve tried everything I can think of to fix this script and it just won’t fix.

I’ve identified that GetAsync keeps returning nil so its not working. I’m also not sure if its the UpdateAsync that’s not updating the data.

I also have another script with a different data store. I’ve disabled that script and tested it to see if the 2 datastores are conflicting but that is not the case because this inventory save script still don’t work.

Someone pls help I’ve ran outta ideas for solutions to this problem.

Update:
I’ve found out that the save request for this script is being “added to queue” could that be the problem? This was what studio said, “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests”

Update2: I’ve found out that the Update Async works by doing this:

local success, error = DataStorage:UpdateAsync(key, function(old)
		return inventoryData
	end)
	
	if success then
		print("Success!")
		
	else
		print("error")
		warn(error)
	end

Now I know that GetAsync is the thing that’s not working does anyone have any ideas on how I could find out what’s wrong with it?

local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local DataStorage = game:GetService("DataStoreService"):GetDataStore("InventorySave")

local toolsFolder = ServerStorage.Tools

game.Players.PlayerAdded:Connect(function(player)
	local key = "Inventory_"..player.UserId

	local inventory = Instance.new("Folder", player)
	inventory.Name = "Inventory"

	local inventoryData = nil

	pcall(function()
		inventoryData = DataStorage:GetAsnyc(key)
	end)
	
	if inventoryData == nil then
		local clonedAxe = toolsFolder.Axe:Clone()
		clonedAxe.Parent = inventory
	else
		for _, name in ipairs(inventoryData) do
			for i, item in ipairs(toolsFolder) do
				if item.Name == name then
					local itemClone = item.Clone()
					itemClone.Parent = inventory
				end
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inventory = player.Inventory
	local inventoryData = {}
	local key = "Inventory_"..player.UserId

	for _, item in ipairs(inventory:GetChildren()) do

		table.insert(inventoryData, item.Name)
	end


	DataStorage:UpdateAsync(key, function(old)
		return inventoryData
	end)


end)


game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end

	wait(5)
end)

your game:BindToClose(function() looks a bit off, you’re really not doing anything with it if you are kicking the player

The function script of game:BindToClose() should be the following:

game:BindToClose(function()
	wait(5)
end)

You should also considering checking if the run service is running InStudio()

Nope, game:BindToClose() halts the server from closing for 30 seconds, so kicking the player would make the game.Players.PlayerRemoving event fire and giving it plenty of time to save data. The only thing weird about that BindToClose is that wait(5) which would always freeze your studio for 5 seconds or for however many seconds you have in it.

For game:BindToClose() it should be

function saveData(player)
	local inventory = player.Inventory
	local inventoryData = {}
	local key = "Inventory_"..player.UserId

	for _, item in ipairs(inventory:GetChildren()) do

		table.insert(inventoryData, item.Name)
	end


	DataStorage:UpdateAsync(key, function(old)
		return inventoryData
	end)
end

game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(saveData)(player)
	end
end)

He don’t have to kick the player decides to leave unless he is in studio.

What? The only reason you want to use :BindToClose() is to KICK the player to fire the PlayerRemoving event which will in turn save the player’s progress, it’s a fail safe. What you said in the first reply is very useless, why would you BindToClose only to execute a wait(5) that would do nothing?

Tbh I took that bind to close part of script from a youtuber named Alvin blox so idk about that.

This would give the game more time to save, I always add around 3 more seconds.

Yeah I know, just leave it as is.

That’s not how BindToClose() works, it automatically waits 30 seconds, it doesn’t care about your wait() in there. Check the API.

I found out the solution. I had 2 data stores and only one data store can work at a time so I just combined the 2 data stores into 1, and saved everything in to one table.