Sorry i tried my best to mess with datastore
please help.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local dataStore = DataStoreService:GetDataStore("INVEN")
local INV = {}
Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new("Folder", player)
inventory.Name = "Inventory"
local data = nil
local success, err = pcall(function()
local data = dataStore:GetAsync(player.UserId)
end)
if success and data then
for i, item in pairs(data) do
item.Parent = inventory
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i, name in pairs(player.Inventory:GetChildren()) do
table.insert(INV,name.Name)
end
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, INV)
end)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, INV)
end)
end
end)
What you are doing is setting a table inside of the script, that means every player will use the same table. Add a folder inside of the player, and everytime he buys something, add that to the folder. Add a
local INV = {}
in plrs.PlayerAdder, plr Removing and BindToClose, then when the player leaves, loop through the plr folder and add everything to the INV table, then save that table through datastore, then, when the player joins, loop through the table and search for the items inside of the table in your item folder (in serverstorage or wherever it is) and clone them to the player’s backpack
I reworked the code and got it to work. For some reason this code
local success, err = pcall(function()
local data = dataStore:GetAsync(player.UserId)
end)
isn’t working
I remade the code and commented out that part and it works. That part is pretty necessary so I’m stuck at the moment. But I will post the code here, But before you use it try to get the success, err pcall working again as it’s pretty important.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local dataStore = DataStoreService:GetDataStore("INVEN")
Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new("Folder", player)
inventory.Name = "Inventory"
local data = nil
--local succes, errormsg = pcall(function()
local data = dataStore:GetAsync(player.UserId)
--end)
--if succes then
if data ~= nil then
if data.Inventory then
for i, item in pairs(data.Inventory) do
local tag = Instance.new("StringValue")
tag.Name = item
tag.Parent = inventory
end
end
end
--end
end)
local SaveData = function(player)
local dataToSave = {}
dataToSave.Inventory = {}
for i, name in pairs(player.Inventory:GetChildren()) do
table.insert(dataToSave.Inventory,name.Name)
print(name.Name)
end
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, dataToSave)
end)
if success then
print("Saved")
elseif err then
warn(err)
end
end
game.Players.PlayerRemoving:Connect(function(player)
SaveData(player)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)