Attempt to index nil with 'Unit'

I have been fixing some bugs and made an error and now I don’t how to fix it. I have been trying to fix an issue where bullets of my gun won’t spawn and now there is an error where it states: attempt to index nil with 'Unit'. Sorry that my code is messy I was going to clean everything up once I was going to fix the bugs but now I can’t clean up as there is an error. Can you please help me?
local code:

mouse.Button1Down:Connect(function()
	if aiming == true then
		if firemode == "auto" then
			if not isShot then
				shooting = true
				isShot = true

				while shooting == true do
					--local mousePosition = mouseModule.GetPosition()
					local mousePosition = mouse.Hit.Position

					fireEvent:FireServer(mousePosition)
					mouse.Button1Up:Connect(function()
						shooting = false
						spreadEvent:FireServer()
					end)
					task.wait(firerate)
					isShot = false
				end
			end

		elseif firemode =="semi" then
			if not isShot then
				isShot = true
				local mousePosition = mouse.Hit.Position
				fireEvent:FireServer(mousePosition)
				spreadEvent:FireServer()
				task.wait(firerate)
				isShot = false
			end
		end
	end
end)

My server code:

local function fire(player, mousePosition)
	if toolEquipped then
		if ammoLeft > 0 and not reloading then
			local origin = firePoint.WorldPosition --Not Position because position in attachments is local not global
			if not origin then return end
			if not mousePosition then return end
			local dir = (mousePosition - origin).Unit
			local endPos = mousePosition
			local startPos = firePoint.WorldPosition
			if combo >= maxCombo then 
				combo = maxCombo
			elseif combo < maxCombo then
				currentSpread += combo
				combo += COMBOADDER
			elseif combo < 0 then
				currentSpread = 0
				combo = 0
			end
			currentSpread += combo
			shooting = true
			local maxOffset
			local Offset
			local dirOffset
			local dirMaxOffset

			if currentSpread > MAXSPREAD then
				Offset = Vector3.new(
					math.random(-currentSpread,currentSpread),
					math.random(-currentSpread,currentSpread),
					math.random(-currentSpread,currentSpread)
				)/100 * 0.25
				dirOffset = Offset+dir
			else
				maxOffset = Vector3.new(
					math.random(-currentSpread,currentSpread),
					math.random(-currentSpread,currentSpread),
					math.random(-currentSpread,currentSpread)
				)/100 * 0.25
				dirMaxOffset = maxOffset
			end
			
			if currentSpread == 0 then
				caster:Fire(origin, dir, vel, castBehavior)
			elseif currentSpread > 0 then
				caster:Fire(origin, dirOffset, vel, castBehavior)
			elseif currentSpread > MAXSPREAD then
				caster:Fire(origin, dirOffset, vel, castBehavior)
			end
			
			sound.playSound("shoot")
			ammoLeft -= 1
			light.Enabled = true
			task.wait(0.05)
			light.Enabled = false
		else
			reload()
		end
	end
end