I’m attempting to make a inventory system that saves items in a folder but it looks like its not creating
the variable when a player joins as I t’s not printing “Added Item”.
--[Varibles]--
local DatastoreService = game:GetService("DataStoreService")
local InventoryDatastore = DatastoreService:GetDataStore("InventoryDatastoreTe")
--[Player Added]--
game.Players.PlayerAdded:Connect(function(player)
local Success, InventoryDataExists = pcall(function()
return InventoryDatastore:GetAsync(player.UserId)
end)
if Success then
print("Data exists for: ".. player.Name.. ", attempting to load.")
local DataFolder = Instance.new("Folder")
DataFolder.Name = "DataFolder"
DataFolder.Parent = player
wait()
if InventoryDataExists then
local Inventory = Instance.new("Folder")
Inventory.Name = "Inventory"
Inventory.Parent = player:WaitForChild("DataFolder")
local InventoryDatastore = InventoryDatastore:GetAsync(player.UserId)
wait()
for _, Item in pairs(InventoryDatastore) do
local ItemVarible = Instance.new("StringValue")
ItemVarible.Name = Item.ItemName
ItemVarible.Value = Item.ItemValue
print("Added Item")
wait()
end
print("Sucessfully loaded: ".. player.Name.."s".. " Data.")
else
print("Data doesn't exist for: ".. player.Name.. ", creating Data.")
local Inventory = Instance.new("Folder")
Inventory.Name = "Inventory"
Inventory.Parent = player:WaitForChild("DataFolder")
local M9 = Instance.new("StringValue")
M9.Name = "M9"
M9.Value = "Common, 100, 1000, True"
M9.Parent = Inventory
end
else
print("Unable to load: ".. player.Name.."s".. " Data.")
end
end)
--[Player Leaving]--
game.Players.PlayerRemoving:Connect(function(player)
if player:FindFirstChild("DataFolder") then
if player:FindFirstChild("DataFolder"):FindFirstChild("Inventory") then
local InventoryTable = {}
local Items = player.DataFolder.Inventory:GetChildren()
for i = 1, #Items do
local Item = Items[i]
local ItemTable = {
ItemName = Item.Name,
ItemValue = Item.Value
}
table.insert(InventoryTable, ItemTable)
end
InventoryDatastore:SetAsync(player.UserId, InventoryTable)
print("Sucessfully saved: ".. player.Name.."s".. " Data.")
else
print("Unable to find: ".. player.Name.."s".. " Data. (Inventory)")
end
else
print("Unable to find: ".. player.Name.."s".. " Data. (DataFolder)")
end
end)
Any reason to its not loading or saving the data?
Also data does exist: