DataStore Isn't Saving A Player's Items

I made a DataStore script that saves a players tools. This for loop isn’t working. I don’t know if seeing the whole script would help.

for _, name in ipairs(inventory or { }) do
		local tool = tools[name]
		print("tools")   
		tool:Clone().Parent = client.Backpack 
		tool:Clone().Parent = inventory_folder -- For saving and loading
		
	end

Whole Script

   local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")

local player_data = DataStoreService:GetDataStore("player_data")

local tools = ServerStorage.ShopItems
local inventories = ServerStorage.Inventories

Players.PlayerAdded:Connect(function(client)
	local key = "client_"..client.UserId
	local inventory = player_data:GetAsync(key)
	
	local inventory_folder = Instance.new("Folder")
	inventory_folder.Name = client.Name
	inventory_folder.Parent = inventories
	print("folder created")
	print(tools)
	for _, name in pairs(inventory or { }) do ---PART THAT ISN'T WORKING
		local tool = tools[name]
		print("tools")
		tool:Clone().Parent = client.Backpack 
		tool:Clone().Parent = inventory_folder -- For saving and loading
		print("tools")
	end
end)

Players.PlayerRemoving:Connect(function(client)
	print("PlayerLeft")
	local key = "client_".. client.UserId
	local tools = {}
	local inventory_folder = inventories[client.Name]
	
	for _, item in ipairs(inventory_folder:GetChildren()) do
		table.insert(tools, item.Name)
	end
	
	player_data:UpdateAsync(key, function(prev)
		return tools
		end)
		
		inventory_folder:Destroy()
	end)

Setup

2 Likes

Mind showing how you defined inventory? and everything else

Yeah I just added the whole script.

Try using pairs rather than ipairs, I’ve had problems with ipairs before in random situations

2 Likes

Under the folder shop items I put another folder for each tool. Could that be the problem?

You must’ve had problems while iterating over dictionaries then, ipairs can’t be used for that due to how it works, errors aren’t thrown when something is used appropriately.

@Play_MazeOfHeck

this part won’t work because if inventory is not-non nil (false or nil), an empty table will be constructed and right after that you check for a value under tools; if nothing exists in a table then tools[v] will be nil, where v is what is returned as a value through the ipairs iterator.

2 Likes

I was following this tutorial


Why does ipairs work in this system?

Thanks

it works bro, the problem is you used robloxstudio to test, robloxstudio doesnt consistently save/load your data
so try it in real game
also if that’s not the case, and if u have errors, you should’ve shown ur errors
how do u know the problem is in loop :thonk:

I did try it in the actual game. I put a print function in the loop and it doesn’t print. Also there are no errors being shown in the output.

EDIT: one thing I did differently from the tutorial was that I inside the tools folder I didn’t put the tools directly I put another folder for each tool

have u tried using pairs instead of thinking the logic of the other guys thing that u copied?

Yeah, I tried that, do I have to change anything else besides changing ipairs to pairs?

i dont think so, but because there is already print debugs, can you tell us which one prints

print(“folder created”) works, its the print right above the for loop

Both of the prints inside the for loop do not print

I also added a print for when the player leaves and that works

I made a print test script: can you show the output?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")

print("Variables")

local player_data = DataStoreService:GetDataStore("player_data")

local tools = ServerStorage.ShopItems
local inventories = ServerStorage.Inventories

Players.PlayerAdded:Connect(function(client)
print("playerjoined")
	local key = "client_"..client.UserId
	local inventory = player_data:GetAsync(key)

for i,v in pairs(inventory) do
print(i,v)
end
	
local inventory_folder = Instance.new("Folder")
	inventory_folder.Name = client.Name
	inventory_folder.Parent = inventories
	print("folder created")
	print(tools)
	for _, name in pairs(inventory or { }) do ---PART THAT ISN'T WORKING
		local tool = tools[name]
		print("tools")
		tool:Clone().Parent = client.Backpack 
		tool:Clone().Parent = inventory_folder -- For saving and loading
		print("tools")
	end
end)

Players.PlayerRemoving:Connect(function(client)
	print("PlayerLeft")
	local key = "client_".. client.UserId
	local tools = {}
	local inventory_folder = inventories[client.Name]
	
	for _, item in pairs(inventory_folder:GetChildren()) do
		table.insert(tools, item.Name)
	end
	
	player_data:UpdateAsync(key, function(prev)
		return tools
		end)
		
		inventory_folder:Destroy()
	end)
print("done")
1 Like

Variables

done

playerjoined

folder created

ShopItems

PlayerLeft

print("playerjoined")
	local key = "client_"..client.UserId
	local inventory = player_data:GetAsync(key)

for i,v in pairs(inventory) do
print(i,v)--Nothing Was Printed
end
	
local inventory_folder = Instance.new("Folder")
	inventory_folder.Name = client.Name
	inventory_folder.Parent = inventories
	print("folder created")

The issue isn’t your for loop, Nothing is printed from inventory meaning its empty or defining that variable is the issue.

1 Like

it’s generally better to follow a tutorial exactly, or certain elements might not work.


this part

is probably the issue, if you use GetAsync() and data doesn’t exist, then nil is returned, so in the code where @Wizard101fire90 loops through inventory, it could potentially error because you just can’t iterate through nil.

Consider altering this part from your original code:

to this

   if inventory then
	     for _, name in ipairs(inventory) do ---PART THAT IS WORKING
		 local tool = tools[name]
         -- etc
   end

you could add an invert statement, (if not inventory then end) to create a default or starting inventory too.

1 Like

I didn’t follow it exactly because I had already made the buying process and how I set it up was different than in the tutorial.
Mine


Tutorial
image

So this is the issue?

If that is the issue then doesn’t that mean there’s is problem with saving the players data when the player leaves?

Possible Points of Failure Include:

Not Saving Correctly
or
Getting the data
or
DataStores in Studio(Dont work the best, try in game)