local DataStoreService = game:GetService("DataStoreService")
local DataStorage = DataStoreService:GetDataStore("Bananas", "Melons", "Slots")
game.Players.PlayerAdded:Connect(function(player)
player:WaitForChild("PlayerGui"):WaitForChild("Gui"):WaitForChild("Inventory")
local PlayerKey = "Id_"..player.UserId
local success, data = pcall(function()
return DataStorage:GetAsync(PlayerKey)
end)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Bananas = Instance.new("NumberValue")
Bananas.Parent = Leaderstats
Bananas.Name = "Bananas"
local Melons = Instance.new("NumberValue")
Melons.Parent = Leaderstats
Melons.Name = "Melons"
local InventorySlots = data.Slots or 0
print(InventorySlots)
if success then
Bananas.Value = data.Bananas or 0
print("Successfully loaded Bananas : "..data.Bananas.." for "..player.Name)
Melons.Value = data.Melons or 0
print("Successfully loaded Melons : "..data.Melons.." for "..player.Name)
for _,Slot in pairs(player.PlayerGui.Gui.Inventory.Frame.InventorySlots.ScrollingFrame:GetChildren()) do
if Slot:IsA("ImageButton") then
if Slot.SlotValue.Value ~= "0" then
Slot.SlotValue = data.InventorySlots
for i, str in ipairs(data.InventorySlots) do
if type(str) == "string" and string.match(str, Slot.Name.."Slot%d%d%d%d?") then
local pos = str:find("t")
Slot.SlotValue = str:sub(pos + 1, pos + 1)
end
end
end
end
end
end
if not success then
print("Could not load save data for "..player.Name)
end
end)
game.Players.PlayerRemoving:Connect(function(player: Player)
local PlayerKey = "Id_"..player.UserId
local success, result = pcall(function()
local InventorySlots
if script.Parent.InventoryPosting.Players:FindFirstChild(player.Name):FindFirstChildOfClass("StringValue") then
local RawInventoryData = script.Parent.InventoryPosting.Players:FindFirstChild(player.Name):FindFirstChildOfClass("StringValue")
InventorySlots = {}
for _,SlotDataCell in pairs(RawInventoryData:GetChildren()) do
table.insert(InventorySlots, SlotDataCell.Name)
end
print("Recieved the following inventory data to save : ", InventorySlots)
end
local PlayerData = {
Bananas = player.leaderstats.Bananas.Value,
Melons = player.leaderstats.Melons.Value,
Slots = InventorySlots,
}
DataStorage:SetAsync(PlayerKey, PlayerData)
end)
if not success then
warn(result) print("Data did not save correctly on player leave")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local PlayerKey = "Id_"..player.UserId
local success, result = pcall(function()
local InventorySlots
if script.Parent.InventoryPosting.Players:FindFirstChild(player.Name):FindFirstChildOfClass("StringValue") then
local RawInventoryData = script.Parent.InventoryPosting.Players:FindFirstChild(player.Name):FindFirstChildOfClass("StringValue")
InventorySlots = {}
for _,SlotDataCell in pairs(RawInventoryData:GetChildren()) do
table.insert(InventorySlots, SlotDataCell.Name)
end
print("Recieved the following inventory data to save : ", InventorySlots)
end
local PlayerData = {
Bananas = player.leaderstats.Bananas.Value,
Melons = player.leaderstats.Melons.Value,
Slots = InventorySlots,
}
DataStorage:SetAsync(PlayerKey, PlayerData)
end)
if not success then
warn(result) print("Data did not save correctly on server shut down")
end
end
end)
I am attempting to create a data store script that allows the saving of inventory, but it keeps putting out the error in the title when i try to add the “Slots” string to GetDataStore. It works fine if i do not have “Slots” put in GetDataStore, and it loads the Bananas and Melons correctly. Is there any way to prevent this. The loop code that sets the inventory slots to the saved values is not done, so ignore that.