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)