Getting "Attempt to Index Nil with 'FindFirstChild'"

Im trying to make custom hotbar. And to hide all i decided to do “for i , v in ipairs(List:GetChildren()) do”. But then , when im trying to access select icon it says attempt to index nil with findfirstchild. Help i dont know how to fix it. I tried all ways i can

1 Like

maybe you could show us more parts of the script?

1 Like

ok then

game:GetService('StarterGui'):SetCoreGuiEnabled('Backpack', false)


--// Variables

local plr = game.Players.LocalPlayer
local char = plr.Character
plr.CharacterAdded:Connect(function(cher)
	char = cher
end)
local hum : Humanoid = char.Humanoid
local backpack = plr.Backpack
local CurrentItem = nil
local LastNum = 0
local ItemList  = script.Parent.List

--// Functions

local function createIcon(name,tool)
	local v = script.Item:Clone()
	v.Parent = ItemList
	LastNum += 1
	v.Name = LastNum
	v.Gui.ToolName.Text = name
	v.Gui.Number.Text = LastNum
	v.WiredWith.Value = tool
	v.Gui.Position = UDim2.new(0.06, 0,1.1, 0)
	v.Gui:TweenPosition(UDim2.new(0.06, 0,0.1, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Sine,1)
	return v
end

--// Handler

backpack.ChildAdded:Connect(function(tool:Tool)
	local Icon = createIcon(tool.Name,tool)
	Icon.Gui.MouseButton1Down:Connect(function()
		if char:FindFirstChild(tool.Name) and char[tool.Name]:IsA("Tool") then
			hum:UnequipTools()
			for i , v in ipairs(ItemList:GetChildren()) do
				if v:FindFirstChild("Gui"):FindFirstChild("Overlay").Visible == true then
					v:FindFirstChild("Gui").Overlay.Visible = false 
				end
			end
		else
			hum:UnequipTools()
			for i , v in ipairs(ItemList:GetChildren()) do
				if v:FindFirstChild("Gui"):FindFirstChild("Overlay").Visible == true then
					v:FindFirstChild("Gui").Overlay.Visible = false 
				end
			end
			hum:EquipTool(Icon.WiredWith.Value)
			Icon.Gui.Overlay.Visible = true
		end
	end)
end)
1 Like

If it doesn’t find “Gui” in v then the next FindFirstChild call will error since you are calling FindFirstChild on nil so just do this

local Gui = v:FindFirstChild("Gui")
if Gui then
	local Overlay = Gui:FindFirstChild("Overlay")
	if Overlay then
		if Overlay.Visible == true then
			Overlay.Visible = false
		end
	end
end

This isn’t tested btw but if it doesn’t work tell me

thank you! this worked for me : D

Ok so i found another way to do that , maybe it would be useful for someone. Replace GetChildren with GetDescendats and check is v is Image or whatever item you are looking for. To check itemType use :IsA()