Hi, ive made a few posts about it before and this should be my last
Im making a module inventory which stores the player data in a module script, however, the template frame isnt being added to the inventory frame and isnt being saved and loaded
server script
local datastoreservice = game:GetService("DataStoreService"):GetDataStore("inventory")
game.Players.PlayerAdded:Connect(function(player)
local UserInputService = game:GetService("UserInputService")
local playerId = "id_"..player.UserId
local List = script.Parent
local module = require(game.ReplicatedStorage.Items)
local b = require(player:WaitForChild("PlayerGui"):WaitForChild("Modules"):WaitForChild("case"))
local inventory_module = require(game.ServerScriptService:WaitForChild("Inventories"))
local template = game.ServerScriptService.Inventories:WaitForChild("Template")
local rStorage = game:GetService("ReplicatedStorage")
local dataFunc = rStorage:WaitForChild("GetData")
local GetSaved = datastoreservice:UpdateAsync(playerId)
if GetSaved then
inventory_module.Inventories[player].contents = GetSaved[1]
end
--we can invoke the function to get the data.
local data = dataFunc:InvokeServer()
local function addItem(player, name, amount, image)
inventory_module:Add(player, name, amount, image)
end
local function removeItem(player, name, amounnt)
inventory_module:Remove(player, name, amounnt)
end
local inventory = inventory_module.Inventories[player]
local items = inventory.contents
for i, v in pairs(items) do
local clone = template:Clone()
clone.Parent = player.PlayerGui.ScreenGui.background.inventory
clone.Name = v
clone.ImageLabel.Image = "rbxassetid://"..module.Items[v].imageID
end
game.ReplicatedStorage.Cases.GetItem.OnClientEvent:Connect(function(unboxed)
addItem(player, unboxed, 1, "rbxassetid://"..module.Items[unboxed].imageID)
local button = template:Clone()
button.Name = module.Items[unboxed].name
button.Visible = true
button.Parent = List
button.ImageLabel.Image = "rbxassetid://"..module.Items[unboxed].imageID
end)
end)
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local function save(player)
local inventory_module = require(game.ServerScriptService:WaitForChild("Inventories"))
local inventory = {}
for i, v in pairs(inventory_module.Inventories[player].contents) do
inventory:SetAsync(player.UserId,v)
end
end
players.PlayerRemoving:Connect(save)
local function onClose()
if runService:IsStudio() or #players:GetPlayers() <= 1 then task.wait(3) return nil end
for _, player in ipairs(players:GetPlayers()) do
save(player)
end
task.wait(3)
end
game:BindToClose(onClose)
module script
local Inventories = {}
local Inventory = {}
function Inventory:Remove(player, name, amount)
if not player or not name then
return -- Not going over this because I already did in the "new" function
end
local inventory = Inventory:Get(player)
if not inventory then
return -- The user doesn't have an inventory, we can't add an item
end
if inventory[name] then -- Check if the user has the item you're giving
if inventory[name] - (amount or 1) < 0 then -- Check if the amount your removing is more then the user has
inventory[name] = nil -- Delete the item completely
else
inventory[name] = inventory[name] - amount or 1 -- Remove the amount or one item.
end
end
end
function Inventory:Add(player, name, amount, image)
if not player or not name then
return -- Not going over this because I already did in the "new" function
end
local inventory = Inventory:Get(player)
if not inventory then
return -- The user doesn't have an inventory, we can't add an item
end
if inventory[name] then -- Check if the user has the item you're giving
inventory[name] = inventory[name] + amount or 1 -- They do, just add the amount or 1
else
inventory[name] = amount or 1 -- They don't, give them the amount of items or 1
end
end
function Inventory:Get(player)
if not player then
return -- Not going over this because I already did in the "new" function
end
return Inventories[player] -- Return the cached inventory!
end
function Inventory.new(player, contents) -- Hey! Our new function, it takes a player and contents.
if not player then
return -- The player that we specified doesn't exist.
end
if not contents then -- We have no contents, let's make some.
contents = { -- Setting our contents to a table
}
end
Inventories[player] = contents -- Assign the users contents to our cache
return Inventories[player] -- return the inventory, (semi useless b/c its just content but still)
end
return Inventory