Function not running despite being called

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)

There are 2 different events which would call InventoryVisuals, both remoteEvents (ToolAddedEvent and CharacterAddedEvent), do neither of them work?

Also where is this script located? I assume it is a local script, so are you sure the script has loaded on the client before the remoteEvents fire on server?

I think i have an idea. Tool added event one does run. I thought only difference is that it runs first and it is the only one that doesn’t have actual function and is just inside the event. Maybe something about remote functions and events dont mix well. The remote function does get the data but it doesn’t return it only for the character added event.

my previous reply false. it is a local script inside playergui and only the characteraddedevent doesn’t work

Remote events and functions are called and respond differently.

-- On client 
Remoteevent.onClientEvent:Connect()
Remotefunction.onServerInvoke = functionToRun --this should have a return value

--On server
Remoteevent:FireClient()
Remotefunction:InvokeClient()

I think in this case they would both need to be remote events (not functions) as you are not returning a value to the server.

1 Like

hexcede help solve code but tahkn u :grin:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.