I wanted to do an Inventory System that saves the ID and then u can get the inv in the next session.
I looked everywhere and i still didnt understand how to do it.
I would be grateful if you helped me.
And if u want to help me more, could you tell me how to like make that the table save numbers that are portraying the numbers, that multiple items can be saved.
local InventoryD = DataStore:GetDataStore("Inv")
local DataStore = game:GetService("DataStoreService")
local Inv = DataStore:GetDataStore("Inv")
local R_INV = game.ReplicatedStorage.Inventory.Events:WaitForChild("Recieve_INV")
local Send_INV = game.ReplicatedStorage.Inventory.Events:WaitForChild("Send_INV")
local Inventory = { -- Example Player Inventory
}
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder",plr)
folder.Name = "Inven"
local success,errorMessage = pcall(function()
local data = Inv:GetAsync(plr.UserId, "InventoryValue", Inventory)
Inventory = data[1]
print(Inventory)
end)
if success then
print("Data successfully loaded!")
end
if errorMessage then
warn(errorMessage)
end
game.Players.PlayerRemoving:Connect(function(plr)
local coins = plr.Ingame.Coins
Inv:SetAsync(plr.UserId.. "Inv", Inventory)
end)
game:BindToClose(function()
for i,plr in pairs(game.Players:GetPlayers()) do
local coins = plr.Ingame.Coins
Inv:SetAsync(plr.UserId.. "Inv", Inventory)
end
end)
end)
R_INV.OnServerEvent:Connect(function(ID, plr)
local success,errorMessage = pcall(function()
local data = Inv:GetAsync(plr.UserId)
print(Inventory)
end)
table.insert(Inventory, ID)
Inv:SetAsync(plr.UserId.. "Inv", Inventory)
print(Inventory)
end)
game.Players.PlayerAdded:Connect(function(player)
Send_INV:FireClient(player, Inventory)
end)
local t = {}
for i, v in pairs(Inventory:GetChildren()) do
table.insert(t, v.Name)
end
game:GetService("DataStoreService"):GetDataStore("InventorySave"):SetAsync(plr.UserId, t)
So how my inventory system works is like with ID’s of the skins, other scripts check the IDs in the skins intvalue from the inventory and then the skins with the ID should go to the inventory.
R_INV.OnServerEvent:Connect(function(InventoryIDs, plr) -- This is firing when Player's inventory has to be saved I guess
local t = {}
for _, ID in pairs()InventoryIDs do
table.insert(t, ID)
end
Inv:SetAsync(plr.UserId, t)
end)
like every skin has 1 ID.
If u buy it u send the ID to the Server script
The event imports the ID number and should save it in a table and save it to the data store
R_INV.OnServerEvent:Connect(function(InventoryIDs, plr) -- This is firing when Player's inventory has to be saved I guess
local t = {}
local SavedIDs = Inv:GetAsync(plr.UserId)
if SavedIDs ~= nil then
for _, ID in pairs(SavedIDs) do
table.insert(t, ID)
end
end
table.insert(t, ID)
Inv:SetAsync(plr.UserId, t)
end)
This is the only way I can think in which it won’t overwrite saved values
R_INV.OnServerEvent:Connect(function(plr, InventoryIDs)
local t = {}
local SavedIDs = Inv:GetAsync(plr.UserId)
if SavedIDs ~= nil then
for _, ID in pairs(SavedIDs) do
table.insert(t, ID)
end
end
table.insert(t, ID)
Inv:SetAsync(plr.UserId, t)
end)
i know this isn’t your issue, but you shouldn’t have players.PlayerRemoving() inside of a players.PlayerAdded() event since you’re making a new connection each time a player joins, which makes it execute multiple times when multiple people are playing. also the same with the :BindToClose() event.
Another thing is that you also have 2 seperate PlayerAdded events, which i recommend you merge into one.