I am making custom inventory system and for some reason the “InventoryVisual” function is not running despite being called.
-- Services
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Events/RemoteFunctions
local GetBackpack = game.ReplicatedStorage.Events.GetBackpack
local ToolEvent = game.ReplicatedStorage.Events.ToolEvent
local ToolAddedEvent = game.ReplicatedStorage.Events.ToolAddedEvent
local CharacterAddedEvent = game.ReplicatedStorage.Events.CharacterAddedEvent
-- Tables
local HotbarKeys = {Enum.KeyCode.One, Enum.KeyCode.Two, Enum.KeyCode.Three, Enum.KeyCode.Four, Enum.KeyCode.Five,
Enum.KeyCode.Six, Enum.KeyCode.Seven, Enum.KeyCode.Eight, Enum.KeyCode.Nine, Enum.KeyCode.O}
-- Variables
local LocalPlayer = Players.LocalPlayer
local PlayerGUI = LocalPlayer.PlayerGui
local HotbarFrame = PlayerGUI.BackpackGui.HotbarFrame
local BackpackFrame = PlayerGUI.BackpackGui.BackpackFrame
local ItemSlotUI = game.ReplicatedStorage.Assets.UI.ItemSlot
local ItemEquipped = false
local LastEquipped
----------------------------------------------------------------
local RecentHotbar = nil
local RecentBackpack = nil
local function Resize_Offset(LogoBase)
local AB_Size = LogoBase.Parent.AbsoluteSize
local LB_Size = LogoBase.AbsoluteSize
return LB_Size
end
local function GetItemsInDictionary(dictionary)
local count = 0
for i, v in pairs(dictionary) do
count += 1
end
return count
end
local function GetBackpackData()
print("Yes it ran")
local BackpackData = GetBackpack:InvokeServer(LocalPlayer)
print("Hmmm")
RecentHotbar = BackpackData.Hotbar
RecentBackpack = BackpackData.Backpack
print(BackpackData)
return BackpackData
end
local function HotkeyFunction(KeyPressed, Gpe)
if not Gpe then
local KeyNumber
for i, key in pairs(HotbarKeys) do
if KeyPressed.KeyCode == key then
KeyNumber = i
break
else
KeyNumber = nil
end
end
-- if the key pressed is valid then it get hotbar data and finds what item is that key, it will then send remote event to equip
if KeyNumber ~= nil then
local Hotbar = GetBackpackData().Hotbar
if GetItemsInDictionary(Hotbar) > 0 then
for i, item in pairs(Hotbar) do
if KeyNumber == item then
if KeyNumber ~= LastEquipped then
ToolEvent:FireServer(item, true)
if ToolEvent.OnClientEvent then
LastEquipped = nil
LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
else
LastEquipped = item
end
else
ToolEvent:FireServer(nil, false)
LastEquipped = nil
end
end
end
end
end
end
end
local function InventoryVisuals(item, position, inHotbar)
print(inHotbar)
if inHotbar then
print("hhello")
local HotbarData = GetBackpackData().Hotbar
local ItemSlot = ItemSlotUI:Clone()
ItemSlot.Name = item
ItemSlot.ItemName.Text = item
ItemSlot.HotbarPos.Value = position
ItemSlot.Parent = HotbarFrame
print(ItemSlot)
local count = 1
table.sort(HotbarData)
if ((#HotbarData / 2) % 1) == 0 then
for i, item in pairs(HotbarData) do
if HotbarFrame:FindFirstChild(i) then
local frameSize = Resize_Offset(HotbarFrame)
local Icon = HotbarFrame:FindFirstChild(i)
Icon.Position = UDim2.fromScale((count / #HotbarData) + 0.05, 0)
count += 1
end
end
else
for i, item in pairs(HotbarData) do
if HotbarFrame:FindFirstChild(i) then
local frameSize = Resize_Offset(HotbarFrame)
local Icon = HotbarFrame:FindFirstChild(i)
Icon.Position = UDim2.fromScale((count - 1) / (#HotbarData - 1) - 0.05, 0)
count += 1
end
end
end
end
end
local function BackpackFunction()
end
-- Events
UserInputService.InputBegan:Connect(HotkeyFunction)
ToolAddedEvent.OnClientEvent:Connect(InventoryVisuals)
CharacterAddedEvent.OnClientEvent:Connect(function()
print("Helloo")
local item = LocalPlayer:WaitForChild("Backpack")
if item:IsA("Backpack") then
print("Yes this is a backpack")
GetBackpackData()
print("do it stop here")
local BackpackData = GetBackpackData()
local Hotbar = BackpackData.Hotbar
print("hiq")
print(BackpackData, "Hiii")
print(Hotbar)
for i, item in pairs(item:GetChildren()) do
InventoryVisuals(item.Name, Hotbar[item.Name], true)
end
end
end)