Hi, I'm sensei and I was about to finish my game which had a data system. There was Cash, Karma and your inventory. Everything saved fine, but there was an issue...
For example, let's say I had 10 apples in my inventory. I remove 7 of them (in server-side, dont worry) and now there were 3 apples left. When I saved, the output printed that it saved it in the table and successfully saved. When I rejoined, I still had 7 apples, even though the saving message said that I saved 3 apples in my inventory.
function save(plr)
dataTable.cash = plr.Cash.Value
dataTable.karma = plr.Karma.Value
dataTable.inventory = {}
for _,item in pairs(plr:WaitForChild("Inventory"):GetChildren()) do
table.insert(dataTable.inventory,item.Name)
print("saved "..item.Name.." into datastore")
end
local success,failure = pcall(function()
datastore:SetAsync(tostring(plr.UserId),dataTable)
end)
if success then
print('data saved')
elseif failure and not success then
print(failure)
warn('data failed to save')
end
for i,v in pairs(workspace:GetDescendants()) do
if v.Name == plr.Name.."_Item" then
v:Destroy()
end
end
end
function load(plr)
local data
local success,failure = pcall(function()
data = datastore:GetAsync(tostring(plr.UserId)) --load
end)
if success and data then
print(data.cash)
print(data.karma)
plr.Cash.Value = data.cash
plr.Karma.Value = data.karma
for i,v in pairs(data.inventory) do
local item = game.ReplicatedStorage.Items:FindFirstChild(v)
if item then
local clone = item:Clone()
clone.Parent = plr.Inventory
print('loaded '..item.Name)
end
end
print("successfully loaded data")
elseif failure and not success then
print(failure)
warn('data did not load')
end
end
(yes i did call these functions) NO IDEA why this is happening... I'm trying to learn datastore2 but it's too complicated for me with this hardcoded script...
Any help is appreciated!
