Hello,
I have been updating my game to use datastore2 and overall it has been a super smooth process. However, I am currently in the process of updating my placement system saves to datastore2. When I save the sterilized information and print it the data is clearly there and saved. I also used the OnUpdate function to ensure the data was actually being updated, which it was. However, when I rejoin and print the same datastore it comes up as nil. Does anyone have any ideas?
UPDATE: I found out it is due to the datastore sending too many requests. However, I do not know how to fix this.
local function encodeCFrame(cf)
return {cf:components()}
end
local function decodeCFrame(cf)
return CFrame.new(unpack(cf))
end
function module.PlayerAdd(player)
local ItemPlacementData = DatastoreModule("key", player)
local ItemNameData = DatastoreModule("key", player)
local ItemsInInventoryData = DatastoreModule("key", player)
local ItemsAmountData = DatastoreModule("key", player)
local Id = player.UserId
local suffixes = PlayerRoomModule.MoneySuffixes()
local PlayerDataFolder = PlayerData:WaitForChild(Id)
local BuildInventory = PlayerDataFolder:WaitForChild("BuildInventory")
if not ItemsInInventoryData:Get() then
for i,v in pairs(Items:GetChildren()) do
local ItemsForNewPlayers = PlayerRoomModule.ItemsForNewPlayers()
if ItemsForNewPlayers[v.Name] then
local Item = Instance.new("NumberValue", BuildInventory)
Item.Name = v.Name
Item.Value = ItemsForNewPlayers[v.Name]
end
end
end
local ItemPlacement = ItemPlacementData:Get()
print(ItemPlacement)
local ItemName = ItemNameData:Get()
if ItemPlacement and ItemName then
for i, Name in pairs(ItemName) do
for a, Placement in pairs(ItemPlacement) do
if i == a then
pcall(function()
local Item = Items[Name]:Clone()
Item.Parent = getRoom(player).PlacedObjects
local baseCFrame = getRoom(player).PlayerRoomBase.CFrame
Item:SetPrimaryPartCFrame(baseCFrame:toWorldSpace(decodeCFrame(Placement)))
end)
end
end
end
end
local ItemsInInventory = ItemsInInventoryData:Get()
local itamo = ItemsAmountData:Get()
if ItemsInInventory and itamo then
for i,Name in pairs(ItemsInInventory) do
for a,Value in pairs(itamo) do
if i == a then
pcall(function()
if BuildInventory:FindFirstChild(Name) then
BuildInventory:WaitForChild(Name).Value = Value
else
local Item = Instance.new("NumberValue", BuildInventory)
Item.Name = Name
Item.Value = Value
end
end)
end
end
end
end
ItemPlacementData:OnUpdate(function(newValue)
print(newValue)
end)
ItemNameData:OnUpdate(function(newValue)
print(newValue)
end)
ItemsInInventoryData:OnUpdate(function(newValue)
print(newValue)
end)
ItemsAmountData:OnUpdate(function(newValue)
print(newValue)
end)
end
function module.PlayerLeave(player)
local ItemPlacementData = DatastoreModule("key", player)
local ItemNameData = DatastoreModule("key", player)
local ItemsInInventoryData = DatastoreModule("key", player)
local ItemsAmountData = DatastoreModule("key", player)
local Id = player.UserId
local PlayerDataFolder = PlayerData:WaitForChild(Id)
local BuildInventory = PlayerDataFolder:WaitForChild("BuildInventory")
local ItemPlacement = {}
local ItemName = {}
local ItemsInInventory = {}
local ItemsAmount = {}
for _,v in pairs(getRoom(player).PlacedObjects:GetChildren()) do
local objectCFrame = getRoom(player).PlayerRoomBase.CFrame:toObjectSpace(v.PrimaryPart.CFrame)
table.insert(ItemPlacement, encodeCFrame(objectCFrame))
table.insert(ItemName, v.Name)
end
for _,i in pairs(BuildInventory:GetChildren()) do
table.insert(ItemsInInventory, i.Name)
table.insert(ItemsAmount, i.Value)
end
ItemNameData:Set(ItemName)
ItemsInInventoryData:Set(ItemsInInventory)
ItemsAmountData:Set(ItemsAmount)
ItemPlacementData:Set(ItemPlacement)
print(ItemPlacementData:Get())
getRoom(player).Owner.Value = "NoOwner"
end