Attempt to index nil with 'MainFrame'

So I tried to make a player invisible by clicking or tapping with the item and uhh this happens
Console:

15:35:01.700  Players.KamuWasTaken.Backpack.Invis Watch.CloakHandler:33: attempt to index nil with 'MainFrame'  -  Server - CloakHandler:33

here are the scripts:
CloakHandler:

local FADE_IN_TIME = 1
local DURATION = 15
local FADE_OUT_TIME = 1
local COOLDOWN = 10

local tool = script.Parent
local canActivate = tool.CanActivate

local function callOnChildren(object, func)
	func(object)
	for _, child in pairs(object:GetChildren()) do
		if not child:IsA("Tool") or child == tool then
			callOnChildren(child, func)
		end
	end
end

local function makeInvisible(object, delta, originalTransparencies)
	if object:IsA("BasePart") or object:IsA("Decal") then
		if not originalTransparencies[object] then
			originalTransparencies[object] = object.Transparency
		end
		object.Transparency = originalTransparencies[object] + delta
	end
end

local function onActivated()
	local originalTransparencies = {}
	originalTransparencies[tool.Handle] = 0
	local character = tool.Parent
	local plr = game.Players:GetPlayerFromCharacter(character)		
	local cloakGui = plr.PlayerGui:FindFirstChild("CloakGui")
	local bar = cloakGui.MainFrame.BarBG.Bar
	
	local fadeInSteps = FADE_IN_TIME * 30
	
	if canActivate.Value then
		canActivate.Value = false
		
		script.Parent.Handle.WooshStart:Play()
		if fadeInSteps <= 1 then
			fadeInSteps = 1
		end		
		for t = 0, fadeInSteps do
			callOnChildren(character, function(object)
				makeInvisible(object, t / fadeInSteps, originalTransparencies)
				makeInvisible(tool, t / fadeInSteps, originalTransparencies)
			end)
			wait(1/30)
		end
		
		tool.Handle.Transparency = 1
	else
		script.Parent.Handle.WooshEnd:Play()
		local fadeOutSteps = FADE_OUT_TIME * 30
		for t = fadeOutSteps, 0, -1 do
			callOnChildren(character, function(object)
				makeInvisible(object, t / fadeOutSteps, originalTransparencies)
				makeInvisible(tool, t / fadeInSteps, originalTransparencies)
			end)
			wait(1/30)
		end
		
		tool.Handle.Transparency = 0
		canActivate.Value = true
		--	tool.Handle.Transparency = 0
		--local fadeInSteps = FADE_IN_TIME * 30
		--if fadeInSteps <= 1 then
		--	fadeInSteps = 1
		--end
		--for t = 0, fadeInSteps do
		--	callOnChildren(character, function(object)
		--		makeInvisible(object, t / fadeInSteps, originalTransparencies)
		--		makeInvisible(tool, t / fadeInSteps, originalTransparencies)
		--	end)
		--	wait(1/30)
		--end
		--tool.Handle.Transparency = 1
	
		--if bar.Size == UDim2.new(0, 0, 1, 0) then
		--	script.Parent.Handle.WooshEnd:Play()
		--	local fadeOutSteps = FADE_OUT_TIME * 30
		--	for t = fadeOutSteps, 0, -1 do
		--		callOnChildren(character, function(object)
		--			makeInvisible(object, t / fadeOutSteps, originalTransparencies)
		--			makeInvisible(tool, t / fadeInSteps, originalTransparencies)
		--		end)
		--		wait(1/30)
		--	end

		--	tool.Handle.Transparency = 0
		--elseif bar.Size == UDim2.new(0, 0, 1, 0) then
			
		--	local originalTransparencies = {}
		--	originalTransparencies[tool.Handle] = 0
		--	local character = tool.Parent		

		--	local fadeInSteps = FADE_IN_TIME * 30

		--	script.Parent.Handle.WooshEnd:Play()
		--	local fadeOutSteps = FADE_OUT_TIME * 30
		--	for t = fadeOutSteps, 0, -1 do
		--		callOnChildren(character, function(object)
		--			makeInvisible(object, t / fadeOutSteps, originalTransparencies)
		--			makeInvisible(tool, t / fadeInSteps, originalTransparencies)
		--		end)
		--		wait(1/30)
		--	end

		--	tool.Handle.Transparency = 0
		--end
	end
	if bar.Size == UDim2.new(0, 0, 1, 0) then
		script.Parent.Handle.WooshEnd:Play()
		local fadeOutSteps = FADE_OUT_TIME * 30
		for t = fadeOutSteps, 0, -1 do
			callOnChildren(character, function(object)
				makeInvisible(object, t / fadeOutSteps, originalTransparencies)
				makeInvisible(tool, t / fadeInSteps, originalTransparencies)
			end)
			wait(1/30)
		end
		
		tool.Handle.Transparency = 0
		canActivate.Value = true
	end
end

tool.Activated:Connect(onActivated)
tool.Equipped:Connect(function()
	if canActivate.Value then
		tool.Handle.Transparency = 0
	end
end)

BackpackHandler(players cant use any item):

local canActivated = script.Parent.CanActivate
local players = game:GetService("Players")
local lPLayer = players.LocalPlayer

local char = lPLayer.Character or lPLayer.CharacterAdded:Wait()	
local cloakGui = lPLayer.PlayerGui:FindFirstChild("CloakGui")
local bar = cloakGui:FindFirstChild("MainFrame"):FindFirstChild("BarBG"):FindFirstChild("Bar")

script.Parent.Activated:Connect(function()
	if canActivated.Value then
		canActivated.Value = false
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
		if bar.Size == UDim2.new(0, 0, 1, 0) then
			game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
		end
		wait(10)
		canActivated.Value = true
	end
end)

char.Humanoid.Died:Connect(function()
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
end)

CloakBarHandler:

local bar = script.Parent

local cloak = 750
local cloakRate = 0


local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local humanoid = char:WaitForChild("Humanoid")

local isCloaking = false

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed or cloak == 0 then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		isCloaking = true
	end
end)

while wait() do
	
		
	if cloak == 0 and isCloaking then
		isCloaking = false
	end

	--and humanoid.MoveDirection.Magnitude > 0
	if isCloaking then
		
		cloak = cloak - 1
		wait(cloakRate)
		
	else
		cloak = cloak + 1
		wait(cloakRate)
	end
	
	
	cloak = math.clamp(cloak, 0, 100)
	
	bar.Size = UDim2.new((1/100) * cloak, 0, 1 ,0)
end

if you guys found the problem please reply with the code(if not is okay), thank you!
also this is the layout:
image

Like the error says, cloakGui is nil
Upon inspecting your script, in line 32, you have initialized cloakGui to:

plr.PlayerGui:FindFirstChild("CloakGui")

But in the screenshot of your Explorer, I see the UI is inside the tool. Maybe you meant to index it from there and parent it to PlayerGui?

local cloakGui = script.Parent:FindFirstChild("CloakGui")
cloakGui.Parent = plr.PlayerGui

Maybe I’m missing something because I didn’t read the other scripts

forgot to send the GuiClone script:

local plr = game.Players.LocalPlayer

script.Parent.Equipped:Connect(function()
	local clone = script.Parent.CloakGui:Clone()
	clone.Parent = plr.PlayerGui
end)

script.Parent.Unequipped:Connect(function()
	plr.PlayerGui.CloakGui:Destroy()
end)

so it can clone the UI when equipped and the UI destroyed when Unequipped

Looking at the Explorer, seems like the UI is being cloned locally (using a LocalScript), and a ServerScript is trying to access the UI. Anything done locally won’t be replicated to the server.

Rather clone the UI in the ServerScript.

it works, but there’s another problem. Players can’t be visible back, do you know how to make players visible back, by running out of energy to be invisible and by clicking again? thank you!

You seem to save all the transparencies in the originalTransparencies table. Wouldn’t making a function that reverts the transparencies work?

local function makeVisible(originalTransparencies)
    for o, t in pairs(originalTransparencies) do
        o.Transparency = t
    end
    table.clear(originalTransparencies)
end

it didn’t work, the player it didn’t go visible

(sorry for not responding)