Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this function to add items to a new inventory not getting called? [solved]

Asked by 5 years ago
Edited 5 years ago

So I have a script & a module script that are meant to detect if the player has an existing inventory when they load into the game & if they don't it creates a new inventory with some starter items, the problem is that it doesn't seem to call the InventoryMod.AddItem function (I was checking a value using print but they didn't output anything). Here is the code from the module script (I have removed some functions that aren't used with creating/loading an inventory):

local InventoryMod = {}

    Refresh = game.StarterGui.ScreenGui.Inventory.Refresh
    Inventories = {}

    function InventoryMod.AddItem(player, item, amount)
        local itemExists = false
        print(itemExists) --doesn't print anything
        for i, v in pairs(Inventories[player.name]) do
            if v["Name"] == item then
                v["Amount"] = v["Amount"] + amount
                itemExists = true
            end
        end
        print(itemExists) --doesn't print anything
        if itemExists == false then
           local NewItem = {["Name"] = item, ["Amount"] = amount}
            table.insert(Inventories[player.name], NewItem)
        end
        Refresh:FireClient(player)
    end

    function InventoryMod.NewInventory(player)
        Inventories[player.name] = {}
        InventoryMod.AddItem(player, "RustyPlate", 10)
        InventoryMod.AddItem(player, "IronPlate", 10)
    end

    function InventoryMod.AddInventory(player, inventory)
        if inventory ~= nil then
            Inventories[player.name] = inventory
        else
            InventoryMod.NewInventory(player)
        end
        print(Inventories[player.name]) --prints 'table: 0000027D7A6AE310'
        print(Inventories[player.name][1]) --prints 'table: 0000027D7A6B0610'
        print(Inventories[player.name][1]["Name"]) --prints 'nil'
        Refresh:FireClient(player)
    end

return InventoryMod

& in another script (again without the code that isn't needed for loading/creating the inventory):

Players = game:GetService("Players")
DataStoreService = game:GetService("DataStoreService")
InventoryStore = DataStoreService:GetDataStore("InventoryDataStore")
InventoryMod = require(workspace.Modules.InventoryModule)

function loadInventory(player)
    local Inventory = InventoryStore:GetAsync(player.UserId)
    InventoryMod.AddInventory(player, Inventory)
    print (Inventory[1]) --prints 'table: 0000027D7A6B0610'
end

Players.PlayerAdded:Connect(function(player)
    loadInventory(player)
end)

BTW sorry if the code is messy I am still a bit of a novice in roblox lua.

1 answer

Log in to vote
0
Answered by 5 years ago

Never mind it turns out that I had solved the problem it's just that it saved an invalid inventory to datastore so it was just loading the invalid inventory instead of making a new one.

Ad

Answer this question