Hello, this is a DataStore that I’ve made that checks for what items the player has in their inventory, saves the Names in a table and then next time they come back those items are sent from the ReplicatedStorage to their inventory.
dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')
game.Players.PlayerAdded:Connect(function(player)
print("Player "..player.Name.." added")
local playerUserId = player.UserId
local data
local success, errorMessage = pcall(function()
data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
end)
if success then
task.wait()
print(player.Name.."'s Accessory Inventory Loaded Succesfully!")
local folder = rs:FindFirstChild('Sample'):Clone()
local isSample = rs:FindFirstChild('ItemStorage')
folder.Name = 'Folder_'..playerUserId
folder.Parent = rs
if folder.Name == 'Folder_'..playerUserId then
for i ,acc in pairs(isSample:GetChildren()) do
print(acc.Name)
print(data)
--// Not matching data names
if acc.Name == data then
print(acc.Name..' found')
end
end
else
--[[
for _,acc in pairs(isSample:GetChildren()) do
acc.Parent = folder
end
for _, child in pairs(folder:GetChildren()) do
child.Parent = player.Inventory
end
print(folder:GetChildren())
]]
return
end
else
warn("Error: "..errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
if player:FindFirstChild("Inventory") then
local accessorySave = {}
for _,acc in pairs(player.Inventory:GetChildren()) do
print(" Obj: "..acc.Name)
table.insert(accessorySave,acc.Name)
end
local success, errorMessage = pcall(function()
contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
end)
if success then
print(player.Name.."'s Accessory Inventory Saved Succesfully!")
else
warn("Error: "..errorMessage)
end
end
end)
however idk why this part doesn’t work
for i ,acc in pairs(isSample:GetChildren()) do
print(acc.Name)
print(data)
--// Not matching data names
if acc.Name == data then
print(acc.Name..' found')
end
end
sorry if I don’t reply I really need some sleep I’ve been coding from 12 PM to 6AM so ill probably reply whenever I’m awake
sorry for slow reply, i was able to get this working, however i still have issues sometimes where even tho the player had items sometimes it just doesnt register
dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')
game.Players.PlayerAdded:Connect(function(player)
--print("Player "..player.Name.." added")
local playerUserId = player.UserId
local inv = player:FindFirstChild('Inventory')
local data
local success, errorMessage = pcall(function()
data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
end)
if success then
--print(player.Name.."'s Accessory Inventory Loaded Succesfully!")
local folder = rs:FindFirstChild('Sample'):Clone()
local isSample = rs:FindFirstChild('ItemStorage')
folder.Name = 'Folder_'..playerUserId
folder.Parent = rs
if folder.Name == 'Folder_'..playerUserId then
--print(data)
for i,v in pairs(data) do
rs:FindFirstChild('ItemStorage')[v]:Clone().Parent = folder
end
for i, child in pairs(folder:GetChildren()) do
child.Parent = inv
end
--print(inv:GetChildren())
else
end
else
warn("Error: "..errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
if player:FindFirstChild("Inventory") then
local accessorySave = {}
for _,acc in pairs(player.Inventory:GetChildren()) do
--print(" Obj: "..acc.Name)
table.insert(accessorySave,acc.Name)
end
local success, errorMessage = pcall(function()
contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
end)
if success then
-- print(player.Name.."'s Accessory Inventory Saved Succesfully!")
else
warn("Error: "..errorMessage)
end
end
end)
basically same code except now i have this
for i,v in pairs(data) do
rs:FindFirstChild('ItemStorage')[v]:Clone().Parent = folder
end
for i, child in pairs(folder:GetChildren()) do
child.Parent = inv
end
May be because the datastore failed for some reason. Try looping your pcall a few times if success is nil, and check if that improves the success rate.
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local accessorySave = {}
local inv = player:FindFirstChild('Inventory')
if inv then
for _,acc in pairs(inv:GetChildren()) do
--print(" Obj: "..acc.Name)
table.insert(accessorySave,acc.Name)
end
local success, errorMessage = pcall(function()
contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
end)
if success then
-- print(player.Name.."'s Accessory Inventory Saved Succesfully!")
print(accessorySave)
else
warn("Error: "..errorMessage)
end
end
end)
yeah i’ve got no clue what the issue is and it works worse now for some reason, this is my first time using DataStore so i have no clue what im exactly doing im kind of just moving stuff around until it works
alright guys, thanks everybody for the help, this code works ( currently, if i see any issues ill make sure to update the code )
dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = player.UserId
local data
local success, errorMessage = pcall(function()
data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
end)
if success then
local folder = rs:FindFirstChild('Sample'):Clone()
local isSample = rs:FindFirstChild('ItemStorage')
folder.Name = 'Folder_'..playerUserId
folder.Parent = rs
if folder.Name == 'Folder_'..playerUserId then
print(data)
for i,v in pairs(data) do
isSample[v]:Clone().Parent = folder
print(folder:GetChildren())
end
for i, child in pairs(folder:GetChildren()) do
child.Parent = player:WaitForChild('Inventory')
print(player.Inventory:GetChildren())
end
else
print('Player_'..playerUserId..' had no items')
end
else
warn("Error: "..errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local accessorySave = {}
local inv = player:FindFirstChild('Inventory')
for i,acc in pairs(inv:GetChildren()) do
table.insert(accessorySave, acc.Name)
end
local success, errorMessage = pcall(function()
contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
print(accessorySave)
end)
end)