Hey, I’m really confused on how to add items to the players inventory. Anyone mind helping?
yeah go on
So, I am not entirely sure what table I need to add my items to. Here is my code:
local DefaultInventory = {
["Default"] = 1
}
Players.PlayerAdded:Connect(function(Plr)
-- // Table Inventory
local TableInventory = DataStore2("TableInventory3", Plr)
TableInventory:SetBackup(3)
TableInventory:BeforeInitialGet(function(Inventory)
local Deserialized = {}
for _, ItemId in pairs(Inventory) do
for Key, Value in pairs(DefaultInventory) do
if Value == ItemId then
table.insert(Deserialized, Key)
end
end
end
return Deserialized
end)
TableInventory:BeforeSave(function(Inventory)
local Serialized = {}
for _, ItemName in pairs(Inventory) do
table.insert(Serialized, DefaultInventory[ItemName])
end
return Serialized
end)
local function CallInvEvent(Value)
ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, TableInventory:GetTable({DefaultInventory}))
end
CallInvEvent(TableInventory:GetTable({DefaultInventory}))
Whenever you need to give a player an item, all you should need to do is Get, insert into the table, then Set.
Have you ever thought of branding datastore2(For example call it Safe Saving) for the use of players?
I haven’t thought about it until now but that doesn’t sound like something I’d be interested in.
I’ve tried this but it still doesn’t want to update the inventory.
ReplicatedStorage.DataEvents.Unboxed.OnServerEvent:Connect(function(Plr, Item)
local TableInventory = DataStore2("TableInventory5", Plr)
local NewTable = TableInventory:GetTable({})
table.insert(NewTable, Item)
TableInventory:Set({NewTable})
for i, v in pairs(TableInventory:GetTable({})) do -- // Doesn't print anything I just added.
print(i)
print(v)
end
end)
-- // Table Inventory
local TableInventory = DataStore2("TableInventory5", Plr)
TableInventory:SetBackup(3)
TableInventory:BeforeInitialGet(function(Inventory)
local Deserialized = {}
for _, ItemId in pairs(Inventory) do
for Key, Value in pairs(DefaultInventory) do
if Value == ItemId then
table.insert(Deserialized, Key)
end
end
end
return Deserialized
end)
TableInventory:BeforeSave(function(Inventory)
local Serialized = {}
for _, ItemName in pairs(Inventory) do
table.insert(Serialized, DefaultInventory[ItemName])
end
return Serialized
end)
local function CallInvEvent(Value)
ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, Value)
end
CallInvEvent(TableInventory:GetTable({DefaultInventory}))
for i, v in pairs(TableInventory:GetTable({})) do
print(i)
print(v)
end
You’re setting the data to a table of your table. Try just using :Set(NewTable)
.
Also, :GetTable({})
is redundant, just use :Get({})
at that point. GetTable is only there for the default values.
Doesn’t seem to work, updated code:
PlayerAdded:Connect(function(Plr)
-- // Table Inventory
local TableInventory = DataStore2("TableInventory7", Plr)
TableInventory:SetBackup(3)
TableInventory:BeforeInitialGet(function(Inventory)
local Deserialized = {}
for _, ItemId in pairs(Inventory) do
for Key, Value in pairs(DefaultInventory) do
if Value == ItemId then
table.insert(Deserialized, Key)
end
end
end
return Deserialized
end)
TableInventory:BeforeSave(function(Inventory)
local Serialized = {}
for _, ItemName in pairs(Inventory) do
table.insert(Serialized, DefaultInventory[ItemName])
end
return Serialized
end)
local function CallInvEvent(Value)
ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, Value)
end
CallInvEvent(TableInventory:GetTable({DefaultInventory}))
for i, v in pairs(TableInventory:Get({})) do
print(i)
print(v)
end
end)
ReplicatedStorage.DataEvents.Unboxed.OnServerEvent:Connect(function(Plr, Item)
local TableInventory = DataStore2("TableInventory7", Plr)
local NewTable = TableInventory:Get({})
table.insert(NewTable, Item)
TableInventory:Set(NewTable)
end)
Change this to :GetTable(DefaultInventory) and try again. You do it a couple of times it seems.
Done that, but still isn’t adding anything to the inventory.
PlayerAdded:Connect(function(Plr)
-- // Table Inventory
local TableInventory = DataStore2("TableInventory9", Plr)
TableInventory:SetBackup(3)
TableInventory:BeforeInitialGet(function(Inventory)
local Deserialized = {}
for _, ItemId in pairs(Inventory) do
for Key, Value in pairs(DefaultInventory) do
if Value == ItemId then
table.insert(Deserialized, Key)
end
end
end
return Deserialized
end)
TableInventory:BeforeSave(function(Inventory)
local Serialized = {}
for _, ItemName in pairs(Inventory) do
table.insert(Serialized, DefaultInventory[ItemName])
end
return Serialized
end)
local function CallInvEvent(Value)
ReplicatedStorage.DataEvents.TableInventory:FireClient(Plr, Value)
end
CallInvEvent(TableInventory:GetTable(DefaultInventory))
end)
ReplicatedStorage.DataEvents.Unboxed.OnServerEvent:Connect(function(Plr, Item)
local TableInventory = DataStore2("TableInventory9", Plr)
local NewTable = TableInventory:Get({})
table.insert(NewTable, Item)
TableInventory:Set(NewTable)
end)
How do you know it’s not being added to the inventory? You only call CallInvEvent the first time.
When I rejoin nothing is in the inventory.
Have you checked your output to make sure it’s actually saving? DataStore2 gives warnings when it intentionally doesn’t save.
Didn’t get a warning in studio.
EDIT: SaveInStudio false I got this - 21:51:06.706 - Data store TableInventory9 attempted to save in studio while SaveInStudio is false.
Check the “SaveInStudio” Value in ServerStorage.
Already tried this, got no warnings.
Can you PM me on this? Realized might be spamming the thread a bit.
In studio in play solo mode if I press the stop button it freezes for about 8 seconds due to the BindOnClose or something in the module when it saves in studio. Any way to stop this or do I just have to not make it save in studio using the variable?
(Keep in mind i am using the module when i took it from your inventory and took it like 1 month after the module came out so mine might be outdated)
Right now you just have to make it not save in studio until the BindToClose studio patch is rolled out. Sorry for the inconvenience.