I’m trying to code a basic inventory system, where the player has different pockets with arrays of their owned items categorized by class, into their respective pockets. But I keep getting this error "attempt to index nil with ‘Visible’ " whenever I try to activate the only working button. This button is what is supposed to turn the item slot visible, as the player owns it. By chance, would anyone be able to help me figure this out? My script will be pasted below, apologies if its a total mess ToT
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local PlayerGui = player.PlayerGui
local globalVariables = require(ReplicatedStorage.Modules.globalVariables)
local movesData = require(ReplicatedStorage.Modules.Data.BattleData.movesData)
local experienceData = require(ReplicatedStorage.Modules.Data.BattleData.experienceData)
local personalitiesData = require(ReplicatedStorage.Modules.Data.BattleData.personalitiesData)
local itemsData = require(ReplicatedStorage.Modules.Data.itemsData)
local mathModule = require(ReplicatedStorage.Modules.Services.MathService.mathModule)
local gatchipediaModule = require(ReplicatedStorage.Modules.Services.GatchipediaService.gatchipediaModule)
local battleModule = require(ReplicatedStorage.Modules.Services.BattleService.battleModule)
local buttonActions = require(game.StarterPlayer.StarterCharacterScripts.Character.buttonActions)
local getInventoryFunction = ReplicatedStorage.remoteFunctions.getInventory
local playerItems = getInventoryFunction:InvokeServer()
local GUI = PlayerGui.Main.GUI
local InventoryScroll = GUI.Bag.InventoryScroll
local ItemSlot = PlayerGui.Templates.ItemSlot
local pockets = {
["Medicine"] = {},
["Items"] = {},
["Discs"] = {},
["Move Manuals"] = {},
["Fruits"] = {},
["Key Items"] = {},
}
repeat
repeat
wait()
until globalVariables.itemsReady
until player
scrollText = function(gui, text, thing)
for i = 1, #text do
gui.Text = string.sub(text, 1, i)
wait(0.05)
end
if thing == nil then
wait(0.55)
end
end
changeVisible = function(thing, isa, bool)
for i,v in ipairs(thing:GetChildren()) do
if v:IsA(isa) then
v.Visible = bool
end
end
end
hash = function(num)
return (num + 4) * 8
end
dehash = function(num)
return (num - 32) / 8
end
HPCalculation = function(level, up, tp, gatchi)
return(up + 2 * globalVariables.Gatchimon[gatchi].Stats.HP + tp / 4) * 100 + 10 + level
end
otherStatCalculation = function(basestat, level, up, tp, personality, gatchi)
return((up + 2 * globalVariables.Gatchimon[gatchi].Stats[basestat] + tp / 4) * level / 100 + 5) * personality
end
local Stats = { "HP", "STA", "ATK", "DEF", "MA", "SPD" }
determinePersonality = function(stat, personality)
if personalitiesData[stat .. "Up"][personality] then
return 1.1
elseif personalitiesData[stat .. "Down"][personality] then
return 0.9
else
return 1
end
end
--Initalize the inventory
local currentPocket = "Medicine"
local function loadItems()
for _, itemData in pairs (itemsData) do
local newItem = ItemSlot:Clone()
if itemData.Class == "Medicine" and currentPocket == "Medicine" then
newItem.Visible = false
newItem.Parent = InventoryScroll
newItem.NameButton.Text = itemData.DisplayName
newItem.ItemIcon.Image = itemData.Image
newItem:AddTag(itemData.Class)
newItem.ItemIcon:AddTag(itemData.Class)
buttonActions.invItem(newItem.NameButton)
elseif itemData.Class == "Item" and currentPocket == "Items" then
newItem.Visible = false
newItem.Parent = InventoryScroll
newItem.NameButton.Text = itemData.DisplayName
newItem.ItemIcon.Image = itemData.Image
newItem:AddTag(itemData.Class)
newItem.ItemIcon:AddTag(itemData.Class)
buttonActions.invItem(newItem.NameButton)
elseif itemData.Class == "CaptureDisc" and currentPocket == "Discs" then
newItem.Visible = false
newItem.Parent = InventoryScroll
newItem.NameButton.Text = itemData.DisplayName
newItem.ItemIcon.Image = itemData.Image
newItem:AddTag(itemData.Class)
newItem.ItemIcon:AddTag(itemData.Class)
buttonActions.invItem(newItem.NameButton)
elseif itemData.Class == "MoveManual" and currentPocket == "Move Manuals" then
newItem.Visible = false
newItem.Parent = InventoryScroll
newItem.NameButton.Text = itemData.DisplayName
newItem.ItemIcon.Image = itemData.Image
newItem:AddTag(itemData.Class)
newItem.ItemIcon:AddTag(itemData.Class)
buttonActions.invItem(newItem.NameButton)
elseif itemData.Class == "Fruit" and currentPocket == "Fruits" then
newItem.Visible = false
newItem.Parent = InventoryScroll
newItem.NameButton.Text = itemData.DisplayName
newItem.ItemIcon.Image = itemData.Image
newItem:AddTag(itemData.Class)
newItem.ItemIcon:AddTag(itemData.Class)
buttonActions.invItem(newItem.NameButton)
elseif itemData.Class == "Key" and currentPocket == "Key Items" then
newItem.Visible = false
newItem.Parent = InventoryScroll
newItem.NameButton.Text = itemData.DisplayName
newItem.ItemIcon.Image = itemData.Image
newItem:AddTag(itemData.Class)
newItem.ItemIcon:AddTag(itemData.Class)
buttonActions.invItem(newItem.NameButton)
end
--Load the player's items
if playerItems ~= nil then
for itemName, itemData in pairs (playerItems) do
if itemData.uses > 0 then
local item = InventoryScroll:FindFirstChild(itemName)
item.Visible = true
item.UsesLabel.Text = "x" .. itemData.uses
end
end
end
end
end
--Change pockets
local function changePocket(Pocket: "Medicine" | "Items" | "Discs" | "Move Manuals" | "Fruits" | "Key Items")
if "Medicine" then
currentPocket = "Medicine"
loadItems()
elseif "Items" then
currentPocket = "Items"
loadItems()
elseif "Discs" then
currentPocket = "Discs"
loadItems()
elseif "Move Manuals" then
currentPocket = "Move Manuals"
loadItems()
elseif "Fruits" then
currentPocket = "Fruits"
loadItems()
elseif "Key Items" then
currentPocket = "Key Items"
loadItems()
end
end
--Buttons
local MedicineButton = GUI.Bag.MedicineButton
local ItemsButton = GUI.Bag.ItemsButton
local DiscsButton = GUI.Bag.DiscsButton
local MMsButton = GUI.Bag.MMsButton
local FruitsButton = GUI.Bag.FruitsButton
local KeyButton = GUI.Bag.KeyButton
local CloseButton = GUI.Bag.CloseButton
MedicineButton.Activated:Connect(function()
changePocket("Medicine")
print("Changed pocket to Medicine!")
end)


