Saving plots:
function savePlot(player, playersPlot)
local playerItems = {}
for _, item in pairs(playersPlot.Purchases:GetChildren()) do
print(item.Name)
local itemData = {}
local primaryPart = item.PrimaryPart
local position = playersPlot.PrimaryPart.CFrame:inverse() * primaryPart.Position
local direction = primaryPart.CFrame.lookVector
itemData.ItemName = item.Name
itemData.Position = {
position.X,
position.Y,
position.Z,
direction.X,
direction.Y,
direction.Z
}
playerItems[#playerItems + 1] = itemData
end
local user = playerData[player.UserId]
table.insert(user.Items, playerItems)
print('saving total: ', #user.Items)
user.Gold = 50
print(user.Gold)
dataManager:SaveData(player)
print('Save done!')
end
Prints everything correctly, 50 cash, 1 item inside the user.Items in the data.
Loading plots
function loadPlot(player, playersPlot)
print('Load started!')
local user = playerData[player.UserId]
if not user then return end
print(#user.Items)
print(user.Cash)
for _, item in pairs(user.Items) do
local targetModel = item.ItemName
local information = item.Position
local newItem = game.ReplicatedStorage.Items:FindFirstChild(targetModel):Clone()
local position = playersPlot.PrimaryPart.CFrame*Vector3.new(
information[1],
information[2],
information[3]
)
local lookVector = Vector3.new(
information[4],
information[5],
information[6]
)
newItem:SetPrimaryPartCFrame(
CFrame.new(
position,
position + (lookVector*5)
)
)
newItem.Parent = playersPlot.Purchases
print('Loaded done')
end
end
Prints 0 and 0 for the user.Cash and user.Items (and thus not running the for loop)
Saving function
function dataManager:SaveData(player)
local saveJSON = httpService:JSONEncode(playerData[player.UserId])
playerDataStore:SetAsync(player.UserId, saveJSON)
end
This is what gets the data when a player joins (just in case itâs important as well)
function dataManager:GetData(player)
local loadJSON = playerDataStore:GetAsync(player.UserId)
local setData = (loadJSON and httpService:JSONDecode(loadJSON)) or {}
playerData[player.UserId] = updateObject(setData, data)
dataReady = true
end
EDIT I found the problem:
function updateObject(old, new)
local update = {}
for i, v in next, new do
update[i] = (type(v) == 'table' and old[i] and updateObject(old[i], v)) or old[i] or v
end
return update
end
local loadJSON = playerDataStore:GetAsync(player.UserId) or {}
local setData = httpService:JSONDecode(loadJSON) or {}
playerData[player.UserId] = setData--updateObject(setData, data)
If I go setData on playerData, it works. The commented out section is what I had before, and with that it breaks. Problem with removing this is if I add some more values to the data store, players who already have saved data wonât be able to get those new values, as they will load in an old data store.