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)
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.
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.
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
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")
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.
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.